Showing posts with label What's the differences between object and object references?. Show all posts
Showing posts with label What's the differences between object and object references?. Show all posts

Sunday, August 12, 2007

What's the differences between object and object references?

Student s = new Student("Joe",20);
is an object of class Student. Can we say s is an object reference
class Student?


s is not an object, it's a variable which contains a reference to an object.
Objects don't have names, just types and locations in memory (and, of course,
fields and methods). Read your statement as: Create a new Student object in
memory, initializing it with the data sent as arguments to a constructor, and
when created, assign a reference to that object to the Student variable s. s
is a reference or object type variable which may reference a Student object or
an object of any subclass of Student.

Take another statement: int x = s.getValue();

Read this statement as "Go to the object referenced by variable s and execute
its getValue() method. Assign the return from that method to the int variable
x."

Topics