Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Reference and Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reference and Object" Watch "Reference and Object" New topic
Author

Reference and Object

Bindu San
Greenhorn

Joined: Mar 08, 2002
Posts: 5
Hi
I was going thru a Java book. I was confused with the meanings of a reference and an Object . Whats exactly does a reference mean and object ?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

I'll try to explain by way of example and diagram:
Object obj = new Object();
With the above statement, two things are created: 1) a variable that can refer to an Object (obj)
2) a new instance of the Object class.

Your code gains access to the object via the reference 'obj'. When using it to invoke methods and access data members, you can think of the reference obj as if it were the object itself. However, when doing assignments and passing obj as a parameter to other methods, you have to remember that it is just a reference.
For example, if you wrote
Object obj = new Object();
Object x = obj;

You now have two references pointing to the same Object. The assignment does not create a new object but merely copies the reference obj to x, in effect, making both x and obj refer to the same Object. The same principle applies when passing obj as a parameter to a method.
HTH,
Junilu


Junilu - [How to Ask Questions] [How to Answer Questions]
 
 
subject: Reference and Object
 
Similar Threads
No. of Objects & Reference Variables
utterly stumped - NPE thrown when calling a method
Diff b/w ref variable & object ref
can "null" be considered as an object?
Inner Class