Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object refernce variable  RSS feed

 
peterx peter
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI!
I have a little problem with Object refernce variable.
How dose it work? please explain it by example.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question isn't very specific, so I don't know whether you're looking for a 1-sentence reply or a 10-page reply.

Basically, a variable holds a value. For primitives, that value is the primitive value itself. But for objects, that value is a "reference." A reference is basically an address in memory, telling the virtual machine where to find the object itself.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not sure i understand the question. what don't you understand?

when you say


a couple of things happen. you create a reference variable, called myVariable. it can ONLY refer to a MyClass object (or a subclass). this variable is created on the stack.

then, you create a MyClass object, which lives on the heap.

finally, you 'connect' the reference to the object.

what this means is that your reference variable really only tells Java how to get to the actual object on the heap - it does not contain the object itself.

but all this is transparent to you. you don't really have to worry about it, which makes life much easier.

so if that didn't answer your question, ask again, but be more specific.
 
peterx peter
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I know b1 and b2 both have the same value because they
refer to the sme Object(House). but if we have this:

//b1 has been set to null, but b2
//still points to the original object.
my question is if b1=null, but why b2 still points to orignal object(House)
How dose it work? Can you explain with another example.
 
Jody Brown
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By saying House b2=b1, you have defined a second reference to the object that b1 is also pointing to at that point in time. b2 does not point to the reference defined in b1. By making b1's reference null, you are only breaking the reference to the object. You are not destroying the object itself. The object will still exist on the heap because b2's reference to it is maintained.

At least I think that is how it works.
 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is correct.

Here's another analogy. I stand in the street and point at your house. Next, you come and stand next to me. You point to the same thing I am pointing to (your house). Then I get in a taxi and drive away. What are you pointing to now? Your house, of course. The fact that I stopped pointing at it has nothing to do with what you are pointing at.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul's illustration is great! The important thing to recognize is that you are both pointing at the house. You are not pointing at Paul.

When you assign b2 = b1, you are making a copy of the value in b1 and assigning that value to b2. The result is that b1 and b2 independently point to the same address. As Jody mentioned, b2 is NOT pointing to b1.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!