File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes reference variable, instance, object ???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reference variable, instance, object ????" Watch "reference variable, instance, object ????" New topic
Author

reference variable, instance, object ????

atul kashyap
Ranch Hand

Joined: Mar 22, 2001
Posts: 34
This may seem very simple for those who know the difference clearly but can be very very confusing for others. Please those who know may help out -
What is the difference between a reference variable, an instance of a class and an object of a class?
Thanks in advance
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

This is from Deitel & Deitel:
The distinction between a variable and a reference is based on the data type of the identifier (as stated in the declaration). If the data type is a class name, the identifier is a reference to an object and that reference can be used to send message to (call methods on) that object. If the data taype is one of the primitive data types, the identifier is a variable that can be used to store in memory or retrieve from memory a single value of the declared primitive type.
In this quote, identifier refers to myVar in the following declaration: int myVar;

As for objects and instances:
A Class is a definition of a possible object. For now, think of object not in programming terms, but in real life. A Book is a real world object. So a book class would contain information and behaviour of a book. Some examples: Title, Author, Pages, CoverType, Dimensions. These are all items of data that could be useful to someone who wants to use a book. A book might also know how to print itself on the screen, or it might have a behaviour of page turning. So these behaviours equate to methods, like PrintBook() and TurnPage().

But no one can do any of these things (in real life or in programming) until you actually have a book in front of you. You can't turn pages of a book that doesn't exist. So when we want to 'make' a book in programs, we have to instantiate the class. The reference you use to help you find the book (myBook) will then point to an instance of the class... otherwise known as an object. Out in memory somewhere there is a 'real' book object, and myBook is the reference you use to use this object.

Hope that helps.

Hope that helps.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If any sheriffs are around, could you try to edit my post?

When I try (to get rid of the duped "hope that helps" and fix a typo), my post has a huge chunk missing out of the middle.
Amit Badle
Ranch Hand

Joined: Jan 31, 2001
Posts: 41
hi atul,
ur question though being simple is a very important 1 as every other OOP concept depends on how u understand this. lets say u have to prepare tea ..now the most important thing r the ingredients, but just having the ingredients doesn�t give u ur cuppa of tea... u have to prepare ur tea. now from the same ingredients u can make n cups of tea some light some strong (ur choice). and after engaging in such an arduous task to enjoy ur cup of tea u pour it in that special bone china cup u had been gifted.
Suppose v write a program in java to prepare tea first v construct a class which has all the
ingredients and methods to prepare tea. then v take out a cup to pour our tea which is like creating a reference variable of a class. Then v can use this class to prepare I cup or n number of cups of tea.; here v create an instance( allocate memory ) of a class and enjoy it using our cup.
Hope I have helped u atleast a bit.
Amit
atul kashyap
Ranch Hand

Joined: Mar 22, 2001
Posts: 34
Thanks Mike and Amit.
What i understand is:
1. reference variable is a holder that can point to an object of the class or of its subclasses - this is right. fine.
2. instance of a class is same as object of a class???
but my teacher says that instance and object are two different things. According to him:
class a
{
public static void main(String args[])
{
a o; //this is a reference variable - this is right i know
new a(); //this is an instance not an object - problem ??
a o1=new a(); //and this is an object ??
}
}
I told him that this means, if you name an instance then it becomes an object. BUT he objects to it. I am confused.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I would expect that he is using the word instance to mean the particular reference to an object that a variable is holding. Not exactly the way that I would use the word, but if you are in his class I would do it his way.
The valid point that he is trying to make (one that LOTS of folks get hung up on) is that a variable is NOT an object. It is a holder that might have the TYPE of an object, but it lives in the stack and objects live on the heap.
a o; // you created an empty variable. There is no object;
o = new a(); //you created an object on the heap and placed a reference to it in the variable o. o holds a reference to an instance of a;


"JavaRanch, where the deer and the Certified play" - David O'Meara
atul kashyap
Ranch Hand

Joined: Mar 22, 2001
Posts: 34
Thanks Cindy i understand the difference between a variable and an object but the confusion was about instance and object.
Please answer in a "yes" or "no" -
An instance of the class is same as an object of the class?
Thanks
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

my answer is "yes".

I think your teacher is being confusing. When you say "an instance of a class", you can replace that phrase with "object". In other words, I almost find it confusing to say "an object of a class".

But like Cindy says, he's your teacher so just go along with him. But know in your own mind that "an instance of a class" = "an instantiated object"
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In my mind an instance of a class is an object.
atul kashyap
Ranch Hand

Joined: Mar 22, 2001
Posts: 34
thanks a lot to all.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: reference variable, instance, object ????