In this quote, identifier refers to myVar in the following declaration: int myVar;
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.
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.
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.
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:
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.
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;
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"