In Head First Java I cam across these statement.
"There is actually no such thing as an object.
There's only an object reference variable."
Now say I have a class
...// instance variable
public static void main( String args)
test a = new test();
If 'a' is the object reference then where is the object. I understood the concept of classes and objects. But couldn't make out what exactly the above refers.
Originally posted by Ulf Dittmer:
That sentence is a bit misleading (I wonder if it's taken a bit out of context). Of course there are objects - the entity created by "new test()" is an object. The variable "a" contains a reference to that object.
Does that help?
As you said, Of course they are object. Ok. But again you have written the statement "a" contains a reference to that object. I didn't get this. There is only one "a". If "a" is an object then where is the reference, i am confused please help me out.
You push buttons on the remote, and you see the corresponding effects of those button pushes from the TV.
Your remote is the 'reference', and the TV is the 'object'.
You create objects, but you seldom get to actually touch them. Instead you manipulate them through references.
Originally posted by Shivit Agarwal:
I didn't get that sir. In the first reply you said it is object. In the second it is not but reference.
No, I always said that "a" is a reference to an object. But as Anand said, in Java you don't get to work with objects directly (not the way you can in C, anyway). You need a reference to them, and a statement like "a = new test()" assigns a reference to the newly created object to "a".
You can assign this reference to other variables, too. "test b = a;" assigns it to variable "b". Now you have two references, but still only one object.
I'm traveling, and I don't have a copy of HF Java with me Perhaps you could make another post and tell me where in the book we said that? I'm hoping that what you put down was not a direct quote
My guess is that we said something like "objects don't have names - but the things that you use to reference them, the "reference variables" do". Anyway, Ulf's answer is spot on!
As I said, I'm traveling right now and I don't have access to the book. When I get home I'll take a look and give you a more exact response. So, as far as what's true about Java, I totally agree with Ulf's explanation. As far as what we said in the book, I have to see it in context before I can respond.