This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I know the difference between Reference variable & Object.  RSS feed

 
deepikakasi kasi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I know the difference between Refernce variable & object
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object o = new Object();

o = reference variable.
new Object() = object (better: an instance of the class).
[ October 12, 2006: Message edited by: Bauke Scholtz ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,


the reference variable, o in Bauke's example, contains a bit pattern that tells the virtual machine where to find the actual object.
The actual object is the data structure with its methods and variables. It can only be adressed via the reference variable.

Objects are also called instances of their class.


Yours,
Bu.


pm, hey, deepikakasi, your member number is a prime!
 
Atul Shinkar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ALL
Good explanation Burkhard!
I want to explain it in simple ways.

Statement1: Demo var;
where,
Demo - class name
var - is the reference variable that refers to the object's start memory location. At this point, the actual object is not created. It will be created in the next statement.

Statement1: var = new Demo();
This statement contains a "new" keyword which actually creates the object in the memory which contains the instance variables & methods [i.e a data structure as mentioned by Burkhard]. The new returns the reference of this object of Demo class, which should be catched in the reference variable of a Demo class only. [Exception to this statement: We can assign the reference of object of subclass to the reference variable of its superclass, but the vice-versa is not possible!]

Hope Depikakasi, this helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!