Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

About object references

 

 

 

 

 

 

 

 

 

Although, for (almost) all intents and purposes we can treat it as a button! 

 

 

 

 

 

Other details like its properties and any associated methods.

 

What is an object reference?

Apart from primitives , all Java objects are actually references. A primitive, such as an int , boolean or double, is stored directly into a memory location:

When you instantiate an object such as a button:

    Button b = new Button("Press me");

b is not actually a Button object , strictly speaking it's a reference to a button instance (it specifies where in memory the Button and it's associated methods are to be found).

Assume we create a second button object:

    Button c = new Button("Press me not");

Somewhere in physical computer memory these details are stored:

 If we make an assignment such as:

                  c = b            

 We overwrite the reference ( Button c now references the same address that b references ("Press me"). The memory occupied by "Press me not" is no longer referenced and becomes inaccessible storage.

Any methods available to c, such as setColor(blue), will now affect button b ("Press me"), that is:

   b.setColor(Color.BLUE);
    c.setColor(Color.BLUE);

will now affect the same object instance.

Back to top

 

Related: [ Java home | Previous: Data Structures | Next: lists ]

A class defines an Object; when the constructor of the class is called (with the new keyword) a new instance of that object is created.

Often we use the words object and instance interchangeably.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009