Main Page

Previous Next

Defining and Using a Class

To put what we know about classes to use, we can use our Sphere class in an example.

You will be creating two source files. The first is the file CreateSpheres.java, which will contain the definition of the CreateSpheres class that will have the method main() defined as a static method. As usual, this is where execution of the program starts. The second file will be the file Sphere.java that contains the definition of the class Sphere that we have been assembling.

Both files will need to be in the same directory or folder - I suggest you name the directory CreateSpheres. Then copy or move the last version of Sphere.java to this directory.

Try It Out - Using the Sphere Class

Enter the following code for the file CreateSpheres.java:

class CreateSpheres {
  public static void main(String[] args) {
    System.out.println("Number of objects = " + Sphere.getCount());

    Sphere ball = new Sphere(4.0, 0.0, 0.0, 0.0);       // Create a sphere
    System.out.println("Number of objects = " + ball.getCount());

    Sphere globe = new Sphere(12.0, 1.0, 1.0, 1.0);     // Create a sphere
    System.out.println("Number of objects = " + Sphere.getCount());

    // Output the volume of each sphere
    System.out.println("ball volume = " + ball.volume());
    System.out.println("globe volume = " + globe.volume());
  }
}

Compile the source files and then run CreateSpheres, and you should get the output:

Number of objects = 0
Number of objects = 1
Number of objects = 2
ball volume = 267.94666666666666
globe volume = 7234.559999999999

This is the first time we have run a program involving two source files. If you are using the JDK compiler, then compile CreateSpheres.java with the current directory as CreateSpheres using the command:

javac CreateSpheres.java

The compiler will find and compile the Sphere.java source file automatically. If all the source files for a program are in the current directory, then compiling the file containing a definition of main() will compile all the source files for the program.

Note that by default, the .class files generated by the compiler will be stored in the current directory, that is, the directory containing your sourcecode. If you want the .class files stored in a different directory, then you can use the -d option with the Java compiler to specify where they should go. For example, to store the class files in a directory called C:\classes you would type:

javac -d C:/classes CreateSpheres.java

How It Works

The Sphere class definition includes a constructor and the method volume() to calculate the volume of a particular sphere. It also contains the static method, getCount(), we saw earlier, which returns the current value of the class variable count. We need to define this method as static since we want to able to call it regardless of how many objects have been created, including the situation when there are none.

The method main() in the CreateSpheres class puts the class Sphere through its paces. When the program is compiled, the compiler will look for a file Sphere.java to provide the definition of the class Sphere. As long as this file is in the current directory the compiler will be able to find it.

The first thing the program does is to call the static method getCount(). Because no objects exist, you must use the class name to call it at this point. We then create the object ball, which is a Sphere object, with a radius of 4.0 and its center at the origin point, (0.0, 0.0, 0.0). The method getCount() is called again, this time using the object name to demonstrate that you can call a static method through an object. Another Sphere object, globe, is created with a radius of 12.0. The getCount() method is called again, this time using the class name. Static methods are usually called using the class name because in most situations, where you would use such a method, you cannot be sure that any objects exist. After all, the reason for calling this particular method would be to find out how many objects exist. A further reason to use the class name when calling a static method is that it makes it quite clear in the sourcecode that it is a static method that is being called. You can't call a non-static method using the class name.

Our program finally outputs the volume of both objects by calling the volume() method for each, from within the expressions, specifying the arguments to the println() method calls.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor