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
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.
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)
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.
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.
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.
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.