• Once you've created an object, you probably want to use it to use the value of one of its fields, change one of its fields, or call one of its methods to perform an action.


  • Referencing an Object's Fields Object fields are accessed by their name. You must use a name that is unambiguous.


  • You may use a simple name for a field within its own class. For example, we can add a statement within the Rectangle class given below that prints the width and height:


  •  
    
    class Rectangle
    {
    private int width, height;
    void print()
    {
    System.out.println("Width and height are: " + width + ", " + height);
    }
    }
    
    
  • In this case, width and height are simple names.


  • Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name, as in:


  •  
    objectReference.fieldName
    
    
  • For example, the code in the CreateObjectDemo class given below is outside the code for the Point class. So to refer to the x, y fields within the Point object named obj, the CreateObjectDemo class must use the names obj.x, obj.y respectively.


  • The program uses two of these names to display the x and the y of obj:


  •  
    class Point
    {
    public int x, y;
    }
    
    class CreateObjectDemo
    {
    public static void main(String[] args)
    {
    Point obj = new Point();
    obj.x=10;
    obj.y=20;
    System.out.println("Width of Point obj: "  + obj.x);
    System.out.println("Height of Point obj: " + obj.y);
    
    }
    }
    
    
    
  • Attempting to use the simple names width and height from the code in the CreateObjectDemo class results in a compiler error.


  • Later, the program uses similar code to display information about obj2. Objects of the same type have their own copy of the same instance fields.


  •  
    class Point
    {
    public int x, y;
    }
    
    class CreateObjectDemo
    {
    public static void main(String[] args)
    {
    Point obj = new Point();
    obj.x=10;
    obj.y=20;
    Point obj2 = new Point();
    obj2.x=50;
    obj2.y=60;
    System.out.println("Width of Point obj: "  + obj.x);
    System.out.println("Height of Point obj: " + obj.y);
    System.out.println("Width of Point obj2: "  + obj2.x);
    System.out.println("Height of Point obj2: " + obj2.y);
    
    }
    }
    
    
    
  • Thus, each Point object has fields named origin, width, and height.


  • When you access an instance field through an object reference, you reference that particular object's field.


  • The two objects obj and obj2 in the CreateObjectDemo program have different x and y fields.


  • To access a field, you can use an object or you can use any expression that returns an object reference.


  • Recall that the new operator returns a reference to an object.


  • So you could use the value returned from new to access a new object's fields:


  •  
    
    int x = new Point().x;
    
    
  • This statement creates a new Point object and immediately gets its x.


  • Note that after this statement has been executed, the program no longer has a reference to the created Point, because the program never stored the reference anywhere.


  • The object is unreferenced, and its resources are free to be recycled by the Java Virtual Machine.


  • Calling an Object's Methods You also use an object reference to invoke an object's method. You append the method's simple name to the object reference, with an intervening dot operator (.). Also, you provide, within enclosing parentheses, any arguments to the method. If the method does not require any arguments, use empty parentheses.


  •  
    objectReference.methodName(argumentList);
    or:
    
    objectReference.methodName();
    
    
    
    class Rectangle
    {
    private int width, height;
    void print()
    {
    width=10;
    height=20;
    System.out.println("Width and height are: " + width + ", " + height);
    }
    void printArea(int x, int y)
    {
    width=x;
    height=y;
    System.out.println("Area: " + (width*height));
    }
    }
    
    class CreateObjectDemo
    {
    public static void main(String[] args)
    {
    Rectangle obj = new Rectangle();
    obj.print();
    obj.printArea(10,20);
    
    }
    }
    
    
  • The Rectangle class has methos: print() and printArea().


  • These are invoked from class CreateObjectDemo


  •  
    obj.print();
    obj.printArea(10,20);
    
    
    
  • The first statement only invokes print() whereas the second statement calls method printArea(int x, int y). It passes values to printArea(int x, int y).


  • As with instance fields, objectReference must be a reference to an object.


  • You can use a variable name, but you also can use any expression that returns an object reference.


  • The new operator returns an object reference, so you can use the value returned from new to invoke a new object's methods:


  •  
    new Rectangle().printArea(10,20)
    
    
    
  • The expression new Rectangle() returns an object reference that refers to a Rectangle object.


  • As shown, you can use the dot notation to invoke the new Rectangle's getArea() method to compute the area of the new rectangle.


  • Some methods return a value.


  •  
    class Rectangle
    {
    private int width, height;
    int printArea(int x, int y)
    {
    width=x;
    height=y;
    return width*height;
    }
    }
    
    class CreateObjectDemo
    {
    public static void main(String[] args)
    {
    Rectangle obj = new Rectangle();
    int ans = obj.printArea(10,20);
    
    }
    }
    
    
  • For methods that return a value, you can use the method invocation in expressions.


  • You can assign the return value to a variable, use it to make decisions, or control a loop.


  • This code assigns the value returned by getArea() to the variable areaOfRectangle:


  •  
    int areaOfRectangle = new Rectangle().printArea(10,20);
    
    
    
  • Some object-oriented languages require that you keep track of all the objects you create and that you explicitly destroy them when they are no longer needed.


  • Managing memory explicitly is tedious and error-prone.


  • The Java platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you don't have to worry about destroying them.


  • The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.


  • An object is eligible for garbage collection when there are no more references to that object.


  • References that are held in a variable are usually dropped when the variable goes out of scope.


  • Or, you can explicitly drop an object reference by setting the variable to the special value null.


  • Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.


  • The Java runtime environment has a garbage collector that periodically frees the memory used by objects that are no longer referenced.


  • The garbage collector does its job automatically when it determines that the time is right.