Objects
Home ] Up ] [ Objects ] Arrays ] Multidimensional Arrays ] Objects of Type Object ] Strings ]

 

 

Creating an Object

  • Two stages:
    • Declare the variable to hold the reference to an object
    • Use new to create an object and associate it with the reference variable:
Grommet g;
Grommet h = new Grommet();
g = new Grommet();
h = new Grommet();	// What does this do?

Accessing an Object

  • Use the dot (.) operator to access fields of an object:
ComplexNumber c = new ComplexNumber();
c.x = 1.0;
c.y = -1.414;
double m = c.magnitude();

Destroying an Object

  • There is no operator in Java to do the opposite of new (i.e. no equivalent of delete in C++)
  • Java detects when an object is no longer being used (has no references)
  • Java automatically deletes such objects
 

This page was last modified on 02 October, 2007