Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reason for this

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another query.

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

class test
{
...// instance variable
...// methods
}
class test
{
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anuradha Karunamuni
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the memory, the actual test object is in the heap and the reference variable "a" is in the stack. "a" is holding a reference to the test object.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nor cleared. Can anybody please explain elaborately in context to my query ??
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"a" is not an object. It is a reference to an object. If "a" were the actual object, then it could never refer to a different object. But that is easily doable by assigning a different object to a.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get that sir. In the first reply you said it is object. In the second it is not but reference.

Can anybody explain this please ??
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine a TV that has *NO* controls, not even the power button, the volume and channel buttons. All you have is the remote.

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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyybody.

@Ulf Dittmer your last post really helped. Thanks once again.
 
Gaurav Ram
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ulf Dittmer , your post really helps and also cleared my doubt also.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

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!

hth,

Bert
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head First Java
2nd Edition - Cover Java 5.0
Page No. 85

This is quite equivocating for the newbie.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shivit,

So that I don't misunderstand, can you tell me more about what you mean when you say
equivocating
?

Thanks,

Bert
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, for abusing the term. What I mean was newbies may get confuse.
[ March 09, 2008: Message edited by: Shivit Agarwal ]
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shivit -

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.

hth,

Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic