Reputation: 219
What is the difference between
ClassABC objectName = new ClassABC();
and
ClassABC objectName = differentVariable.methodReturnsClassABCTypeValue();
Is the second example still initiated like the first? And if no, what are the differences between these two types of "initilizations"?
If this is confusing, I will give a second example using another Java Class.
Toolkit toolKitObject = Toolkit.getDefaultToolkit();
Dimension screenSize = new Dimension();
and
Toolkit toolKitObject = Toolkit.getDefaultToolkit();
Dimension screenSize = toolKitObject.getScreenSize();
Notice how in the first example I created the Dimension object and did not set any value to it.. but in the second example, I never used new Dimension();
I immediately went ahead and did toolKitObject.getScreenSize();
Upvotes: 1
Views: 1512
Reputation: 5655
At below line
ClassABC objectName = new ClassABC();
its creates new Instance on heap and new reference on stack and assigning this new Objects reference to new reference created. . So with this code we will definately getting new object on heap. so after above line objectName will never be null for sure.
But with this
ClassABC objectName = differentVariable.methodReturnsClassABCTypeValue()
we are creating only new reference on stack. and poitning it to existing Object on heap which might be created/referenced inside method.
so at this stage it will definately create reference on stack. but new Object created on heap or not depends on the implementation of the method.
so after above line objectName can be null or not null depending on whats returned from method. . Hope it clears the things.
Upvotes: 0
Reputation: 22715
Both of your examples end up using the new
operator.
Dimension screenSize = toolKitObject.getScreenSize();
Here's the source code of getScreenSize
you're calling in the above line:
// implementation as per SunToolkit.class
public Dimension getScreenSize() {
return new Dimension(getScreenWidth(), getScreenHeight());
}
You'll see that behind the scenes, it's still calling a new Dimension
(the overloaded version that takes height and width).
Upvotes: 1
Reputation: 12843
There are different ways to create objects in java:
A. Using new keyword This is the most common way to create an object in java. Almost 99% of objects are created in this way.
MyObject object = new MyObject();
B. Using Class.forName() If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
C. Using clone() The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();
D. Using object deserialization Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
E. Using reflection in another way.
this.getClass().getClassLoader().loadClass(“com.abc.myobject”).newInstance();
Upvotes: 13
Reputation: 13187
I like the answer by Achintya, but I have the feeling the answer is way more advanced than the question.
In java, a variable (excluding primitive types, such as integer) references an object in memory.
Therefore, the following snippet creates just one object which is referenced by two different variables:
Object a = new Object();
Object b = a;
The same thing happens in your examples, though in a more hidden way.
The following snippet creates a Toolkit
object. This object contains, among others, a Dimension
object. The second line just creates a reference to that already existing object.
Toolkit toolKitObject = Toolkit.getDefaultToolkit();
Dimension screenSize = toolKitObject.getScreenSize();
Upvotes: 3
Reputation: 2088
Nope.!!
2nd one does NOT create an object. It gets an object created from another method. If that method creates a new Object, then it's fine. If that method uses an existing method, no.
Upvotes: 0