AndreasT
AndreasT

Reputation: 207

Java object construction

I am new to Java and I'd like to figure out why some code is written in one way instead of another. When you construct an object in Java, the syntax is something like

class variable = new class(parameters);

or

class variable;
variable = new class(parameters);

I wonder why the class name has to be invoked twice. As documented on Wikipedia, Python (which I don't know either) follows what according to me is a more intuitive approach, i.e.

variable = class(parameters)

Is it because Java can handle other possibilities?, e.g.

class1 variable = ... class2(parameters)

Thanks in advance.

Upvotes: 2

Views: 189

Answers (6)

Alper
Alper

Reputation: 13220

Java is a static typed programming language. However, Python is a dynamic typed programming language.

Wikipedia has very good explanation here.

Upvotes: 2

AllTooSir
AllTooSir

Reputation: 49362

Because the left hand side can be a reference type and the right hand side it can be any assignable object type.

This assigns the reference variable variable of type class1 an object of class1.

class1 variable = new class1(parameters);

This assigns the reference variable variable of type class1 an object of subclass1, where subclass1 can be a subclass of class1 or an implementation of class1 interface.

class1 variable = new subclass1(parameters);

Python uses Dynamic Typing (Duck Typing) and hence you need not declare a variable.Dynamic typed languages are those in which variable type checking is done at run-time.

Static typed programming languages are those in which variables need not be defined before they’re used. This implies that static typing has to do with the explicit declaration (or initialization) of variables before they’re employed. Java is an example of a static typed language. Statically typed languages that lack type inference (such as C and Java) require that programmers declare the types they intend a method or function to use. This can serve as additional documentation for the program, which the compiler will not permit the programmer to ignore or permit to drift out of synchronization.Static typed languages are those in which variable type checking is done at compile-time.

Python (which I don't know either) follows what according to me is a more intuitive approach

The concern with dynamic typing, like this is :

variable = class(parameters);

In future if by mistake you misspelled the variable name variable with varaible , it would not be caught at compile time , but you can get erroneous output or exception at runtime. With static typing , Java detects such flaws at compile time itself.

Upvotes: 4

Andrzej Doyle
Andrzej Doyle

Reputation: 103777

Because they mean different things - and don't need to be the same.

The class descriptor on the left defines the type of the variable. This constrains what can be assigned to that variable, and also provides some guarantees to callers about what the objects there can do.

The class name when it appears on the right is actually the "method name" of a constructor. You're calling a method which creates a new object. The object you create can be anything, so long as it's assignable to the type of the variable.

So for example, you can do this:

Object foo = new String("bar");

or this:

Collection x = new ArrayList();

(though in practice you'd probably want to use generic parameters on that last one - I've left them out here so as not to confuse the question of classes.)

Upvotes: 3

blackpanther
blackpanther

Reputation: 11486

The left hand side (reference definition type) is a reference to an object or instance that can be the type of that class or any subclass of that reference type. Also, in accord with the following reference Python is a dynamic type programming language where every variable name (unless it's null) is bound to only an object. Java, on the other hand, is a statically-typed programming language.

Upvotes: 1

Sky
Sky

Reputation: 709

SomeClass variable;
variable = new SomeClass(params);

The first line declares a variable and states that it is of type SomeClass. At this point the variable is empty and doesn't reference anything.

The second line creates a new object of type SomeClass and stores a reference to it in variable.

Writing both things in one line like this:

SomeClass variable = new SomeClass(params);

can be done to make things shorter. But at the end there is always a variable declaration and an object construction. Variables must always be of some type, you can't declare a variable without type (Java is strongly typed).

Upvotes: 1

Arif Samin
Arif Samin

Reputation: 257

Class name has to be invoked twice because you have to specify the type of the instance. You can also do things like this when creating new instance:

public class Test extends Test1 {}
...
Test1 test = new Test(parameters);

Doing things like this will determine which class' variables and methods you can actually access while maintaining stuffs such as class fields.

Upvotes: 1

Related Questions