It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Reason for this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reason for this" Watch "Reason for this" New topic
Author

Reason for this

Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
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.


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41188
    
  45
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?


Ping & DNS - my free Android networking tools app
Anuradha Karunamuni
Ranch Hand

Joined: Dec 08, 2007
Posts: 64
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.


"Simplicity is the ultimate sophistication..." ~Leonardo da Vinci~
SCJP 1.4
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
nor cleared. Can anybody please explain elaborately in context to my query ??
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
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
Marshal

Joined: Mar 22, 2005
Posts: 41188
    
  45
"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

Joined: Feb 28, 2008
Posts: 82
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

Joined: Aug 22, 2006
Posts: 257

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.


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41188
    
  45
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

Joined: Feb 28, 2008
Posts: 82
Thanks everyybody.

@Ulf Dittmer your last post really helped. Thanks once again.
Gaurav Ram
Ranch Hand

Joined: Feb 23, 2008
Posts: 32
Thanks a lot Ulf Dittmer , your post really helps and also cleared my doubt also.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8806
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Head First Java
2nd Edition - Cover Java 5.0
Page No. 85

This is quite equivocating for the newbie.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8806
    
    5
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

Joined: Feb 28, 2008
Posts: 82
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

Joined: Oct 14, 2002
Posts: 8806
    
    5
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
 
 
subject: Reason for this
 
Similar Threads
StackOverFlow Exception help
Keyword this
doubt in has-a relationship
Mupltiple or single object(s) can be createdof a class: contradiction in this statement
K&B - Statement, Cannot understand