Java Classes, Objects

Creating Objects

Objects are created from a class by using the new operator, and they must be attached to a reference variable. Two steps:
  1. Declare the object reference variable
  2. Create the object with the new operator and attach it to the reference variable

Format

  ClassName objectReference;
  objectReference = new ClassName();

(or combined into one statement)

  ClassName objectReference = new ClassName();
Examples:
  Circle myCircle;		// where Circle is a class
  myCircle = new Circle();

  Dog fido = new Dog();		// where Dog is a class
Caution: Since the name used to refer to an object is a reference variable, and not the object itself, it is important to note that any assignments done on such a variable are just on the reference. For example, if we create two objects, and then assign their variables together:
  Circle c1 = new Circle();
  Circle c2 = new Circle();

  c1 = c2;
... the last statement (c1 = c2) does not copy circle c2 into c1. Instead, it copies the reference varaible c2 to c1, which means that both reference variables are now referring to the same object (the second one, c2).

Using Objects

Once an object is created, access the object's internal methods and data with the dot-operator. Format:
  objectReference.data
  objectReference.method(arguments)   // a call to a method
Example:
  Circle c1 = new Circle();
  c1.radius = 10;		// access radius instance variable

  // compute and print the area with the findArea method
  System.out.print("Area = " + c1.findArea());

Constructors

Objects as Method Parameters

Class Variables and Methods -- static vs instance

The modifier static can be used on variables and on methods To make a class variable constant, add the keyword final as a modifier on the declaration. It's better to make your constants also static -- since the value won't change, it's more efficient to have one variable shared by the entire class.

Example

 class Student
 {
   private int testGrade;		// instance variable (non-static)
   private static int numStudents = 0;	// static variable (class variable)
   private final static int pointsPossible = 100;       // class constant

   public Student()
   {   testGrade = 0;   }

   public void setGrade(int gr)
   {   testGrade = gr;  }

   public int getGrade()
   {   return testGrade;  }

   public static void incrementNumStudents()
   {   numStudents++;   }

   public static int getNumStudents()
   {   return numStudents;  }  

 }
In this sample code: Student.java - You can get a copy of this code example here, along with a small sample main() program that illustrates some calls.

The Keyword this

Fraction class example

In my COP 3330 course, I typically use a Fraction class as my first full C++ class illustration. Here's the corresponding class in Java.

Arrays of Objects

Creating an array of objects is a little trickier than an array of a primitive type.
  1. Create an array using similar syntax to primitive types, but use the class name instead of the primitive type name:
      Student[] list = new Student[10];
    
    This only creates an array of reference variables -- references for type Student
     
  2. Create the individual objects with new, and attach to the reference variables (the array positions). This can be done separately:
      list[0] = new Student();
      list[1] = new Student();
    
    but it's easier with a loop (as long as you are using the same constructor for each object):
      for (int i = 0; i < list.length; i++)
        list[i] = new Student();
    
    Each list[i] is the reference to an object now.

More class examples in Deitel Chapter 8