• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference vs Instance Object Terms

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody clear up to me about term Reference and Object
a little bit more ...
snip:
class Dad
{
Dad()
{
}
public void method()
{
System.out.println( "dad-method");
}
}
class Kid extends Dad
{
Kid()
{
}
public void method()
{
System.out.println( "kid-method");
}
}
public class TestRun
{
public static void main( String[] argv )
{

Dad d = new Dad();
Kid k = new Kid();
// here the confusion, is r a reference to the Kid's object ???
// can some body explain carefully for me please
// why is this calling the Kid's method
// and all that term reference and object in this
// case
Dad r = new Kid();
r.method();
}
}
Thanks in advance
widjajaf
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, r is an Dad object instance but holds a reference to the kid object. And we also have method overriding here. As we know, method use dynamic binding (or runtime binding), this means the real method called in the runtime is determined by the real reference in one reference variable. In this example, when calling r.method(), the runtime would lookup in the kid object and find the overriding method in kid object and call it accordingly.
Do I make it clear
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi widjajaf,
Java has two types of variables: primitive and reference.
Primitive types (int, char, boolean, long, etc) have a pre-set memory size. For example, all 'int' values occupy 32-bits in memory. A primitive variable contains the data assigned to it. For example in <code>int i = 5</code> the memory associated with 'i' contains the value '5'.
Objects do not, cannot, have a pre-set size. They can require small areas of memory or very large areas. On the other hand, a memory address or reference can have a pre-set size.
The language makes use of this fact by storing an objects memory address in a variable instead of trying to hold the object itself. For example in <code>Object obj = new Object();</code> the object itself is created in an empty spot in memory and then the memory address for that object is stored in the variable <code>obj</code>. <code>obj</code> is a 'reference variable' because it refers to an object rather than being the object itself.
In your example, 'r' is a 'reference variable' that holds the memory address for the <code>new Kid()</code> object. When <code>r.method()</code> is encountered the system knows it has to go to the memory address stored in 'r' to locate the actual object.
Hope that helps.
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited October 22, 2001).]
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Mar:
In this case, r is an Dad object instance but holds a reference to the kid object.


hi Alan
'r' is not an object instance, it is object refernece of type Dad, which is holding reference to Kid's object.
And Jane thanks a lot, I knew about references and objects but still ur description has now made everything crystal clear.
Thanks a lot

------------------
Regards
Ravish
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jane thanks for yr prev reply...

now, am I correct to say these?
1. object = object or an instance
2. reference = object reference or
constant pointer to an object instance or
the memory holding the instance
3. object instance != object reference
4. When creating object with "new" the instance Object of that
class is created, but "No reference" always needed ( as in
anonymous class )
5. if( r instanceof Dad ) will return true in this case.
6. Dad r = new Kid()
Becos 'r' is just a memory address, we can assign it with any
new instance (sub class)
7. But Kid kid = new Dad(); will not "compiled". *static binding*
so compiler is checking for 'Kid' reference type when
creating an instance with 'new' operator.
thanks a lot for all your pointers

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic