Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object vs Reference in Java

Tags:

java

In Java, if I declare,

MyClass obj;

Is obj called a "reference" or an "object". I am not instantiating class here.

like image 374
ajay Avatar asked Mar 01 '11 21:03

ajay


People also ask

What is the difference between reference and object in Java?

What is difference between references and objects in java? A reference is an entity which provides a way to access object of its type. An object is an entity which provides a way to access the members of it's class or type. Generally, You can't access an object without a reference to it.

Is object and reference variable same?

A reference variable is a variable that points to an object of a given class, letting you access the value of an object. An object is a compound data structure that holds values that you can manipulate. A reference variable does not store its own values.

Is object a reference type in Java?

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Following are the reference types in Java. class types − This reference type points to an object of a class.

What is the difference between object instance and reference?

Reference points to the Object. Instance is the copy of the Reference that points to object at a point of time. Refrence is a variable that points the objects. Object is the instance of the class that have some memory and instance is a variable & methods that object have.


2 Answers

obj is a Reference to an instance of MyClass.

Currently, that Reference is NULL because you haven't assigned it to refer to any instance.

Technically MyClass must be a subclass of Object, so it is possible to say that obj is a Reference to an instance of Object as well.

like image 72
rfeak Avatar answered Oct 13 '22 18:10

rfeak


Reference: A variable that points to some object in memory. It is stored in stack they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.

Object: An instance of class that is created dynamically. It is stored in heap

Example:

MyClassI aObj,aObj1;

aObj=new MyClass2();

At first line aObj and aObj1 are references

At second line aObj referencing to object of MyClass2(New operator creates an object of Myclass2 and its address is assigned to aObj).

To understand even better consider a class Car which has driverName as a member.

Car c1,c2;

c1.driverName="Andrew"
c2.driverName="Gabriel"

System.out.println(c1.driverName);//gives Andrew
System.out.println(c2.driverName);//gives Gabriel

c1=c2;

c2=null;

// gives gabriel because the address of c2 is copied to reference c1.
// the object is not nullified because c2 is just a reference when 
// assigning null the address that is stored on c2 in nullified not 
// the object it points..

system.out.println(c1.driverName);
like image 41
Durai Amuthan.H Avatar answered Oct 13 '22 18:10

Durai Amuthan.H