4

Ok, so what happens when you do this.

A a1=new A();

A a2=new A();

A a3=new A();

I upload two pictures on how I imagine it being like. Can you tell me what picture is true?

First picture: enter image description here

Second picture: enter image description here

I always thought the first picture is true, but now I don't really know, and I suspect the second to be true.

Also, can you explain to me what each side does? Like, what does "A a1" do and what does "new A()" do?

Thanks.

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
ABCD123
  • 137
  • 1
  • 6
  • Any time you see `new`, it's a new object, a new reference. – Sotirios Delimanolis Oct 06 '13 at 16:38
  • The second picture is a valid representation. `new A();` creates an instance of memory somewhere on the heap. You declared multiple variables and initialized them each. Each of those variable's hold a reference to each instance of memory. – Rohan Oct 06 '13 at 16:43
  • I think you should read more carefully what a class and object means. Most of the time the second picture is the correct one. Every time you use `new` you create new instance of the class so even though it shares common attributes with the other instances it's still unique. – Leron Oct 06 '13 at 16:44

10 Answers10

12

new A() will call the no param constructor of class A and will create a new memory object.

A a1=new A();  // new memory object created

A a2=new A(); // new memory object created

A a3=new A(); // new memory object created

There are three different objects that are getting created, so the SECOND picture is true.

For the FIRST picture to be true, the declaration of references should be something like this:

A a1=new A(); // new memory object created

A a2=a1; // a2 points to the same memory object as a1 does

A a3=a1; // a3 points to the same memory object as a1 does

Here only one object(new used only once) is created but all the three references point to it.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

No, the second picture is true. Because each time you create a new object with a separate reference, it becomes a separate pointer to a separate instance in memory.

A a1 = new A(); // a new instance of A is created in memory and can be referenced by a1.
A a2 = new A();

Even though a1 and a2 are of the same type, it does not mean that they point to, or reference, the same object instance in memory.

But, if a1 = a2; is ever executed, now, both a1 and a2 reference or point to the same A object instance in memory.

blackpanther
  • 10,998
  • 11
  • 48
  • 78
2

This is a variable declaration;

A a1;

You declare a new variable called a1 of type A.

a1 = new A();

You assign to a1 the value that results from new A(), which is a new object of type A.

You can do both things at once as an initialization:

A a1 = new A();

Each time you call your class constructor, you get a different and separated instance of your class. No instances know about the others, they lie in different regions of memory.

By the way, those are very very basics of programming, so get a good reading and happy learning.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
2

Go through the images, hope this helps.

Refer Image

Refer image

Rupesh
  • 2,627
  • 1
  • 28
  • 42
1

First picture is true for Strings( due to string pooling, an optimisation for strings known to be identical at compile time):

String a1="s";
String a2="s";
String a3="s";

The second one is true for:

A a1=new A();
A a2=new A();
A a3=new A();

If you want behaviour like String has - you should implement Flyweight pattern.

Community
  • 1
  • 1
Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
  • Its worth noting that the first is true only because of string pooling, an optimisation for strings known to be identical at compile time – Richard Tingle Oct 06 '13 at 16:44
1

The second picture is true. Each time you write new keyword following by constructor the new instance of class is created. Each time you write someVariable = ... the result of the expression from the right is assigned to the variable, i.e. the new reference is created.

So,

A a1 = new A(); // creates one object and assigns reference to a1
A a2 = new A(); // creates another object and assigns reference to a2
A a3 = a2; // just create yet another reference to previously created object 
a1 = new A(); // creates new object and assigns its reference to the same variable that was previously used, so the previous object is "lost" now and can be garbage collected. 
AlexR
  • 114,158
  • 16
  • 130
  • 208
1

Whenever you see a

ABC a1 = new ABC();

it means a new object is created and using a1 you can access it.

It is exactly similar to going to a bike shop and aking them to get a *new bike *. Every time you say new they will get a new bike for you.

Meaning of both sides:

Right side means you have a new object of type on the right side.

The left side says you want to keep a variable of type on the left side.

So you can have different types of both sides but there is a contract or condition that you have to take care. The condition is that the type on the left side should be either a super class of the type on the right side or the left side is an interface and the right side type implements it.

Example:

A a1 = new AB();
A a2 = new BC();

and both AB and BC either are subclass of A or A is an interface which will be implemented.

Now after creating an object when you assign it to the left side is exactly similar to having a baloon (newely created object) and attaching a string (a1) to it. This way you can attach multiple string to one baloon i.e.,

A a1 = new A();
A a2 = a1;

Bithe a1 and a2 point to the same object.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83
1

2nd picture is correct.

writing new is always create a newly object in heap.

in real world comparison

A a=new A();

the above code can be similar to

new A() ------------------ birth of a child

A a ------------------ naming of that child(i.e. Ram,John)

vikas singh
  • 131
  • 1
  • 2
  • 10
0

The second picture is correct. Each of the three statements is creating a reference (A a1 =), and an actual object in memory (new A()). Throw out the first picture :)

musical_coder
  • 3,886
  • 3
  • 15
  • 18
0

2nd one is correct because whenever an object is created, it´s constructor is being called only once.

Here the three different objects are created by the constructor calls. So the constructor is called three times.

Whereas an ordinary object method may be called any number times for a given object, not the constructor.

user3666197
  • 1
  • 6
  • 50
  • 92