aspose file tools*
The moose likes Beginning Java and the fly likes Difference between instantiating and creating an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between instantiating and creating an object" Watch "Difference between instantiating and creating an object" New topic
Author

Difference between instantiating and creating an object

Praveen palukuri
Ranch Hand

Joined: Feb 10, 2005
Posts: 65
hi,
would u please tell me the difference between the following:

Class Test1{....}
Class Test2
{
Test1 t1;
Test1 t1 = new Test1();
}
what is the difference between above 2 statements?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

First of all, "instantiating an object" and "creating an object" is the same thing. Different words with the same meaning.

About your statements; do you mean the difference between these two?

Test1 t1;
Test1 t1 = new Test1();

In the first line, you declare a variable t1 of type Test1. You are not initializing it, and you are not instantiating/creating an object. Because it's a member variable (of class Test2), it will get the default value null.

In the second line, you declare a variable t1 of type Test1 and you instantiate/create a new Test1 object, and you initialize the variable t1 with this object (i.e. t1 is a reference to the newly created object).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The first statement is only declaring a reference. As it is a field, it will get assigned the default value of null. No object gets created.

The second statement is declaring a reference, creating (=instanciating) an object via new, and letting the reference reference that object (using the assignment operator).

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between instantiating and creating an object