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:
Second picture:
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.
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.
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.
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 .
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.
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 ie,
A a1 = new A();
A a2 = a1;
Bithe a1
and a2
point to the same object.
Go through the images, hope this helps.
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(ie Ram,John)
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 :)
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, ie 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.
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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.