Main Page

Previous Next

Defining Classes

To define a class you use the keyword class followed by the name of the class, followed by a pair of braces enclosing the details of the definition. Let's consider a concrete example to see how this works in practice. The definition of the Sphere class we mentioned earlier could be:

class Sphere {
  static final double PI = 3.14;  // Class variable that has a fixed value
  static int count = 0;           // Class variable to count objects

  // Instance variables
  double radius;                  // Radius of a sphere

  double xCenter;                 // 3D coordinates
  double yCenter;                 // of the center
  double zCenter;                 // of a sphere

  // Plus the rest of the class definition...

You name a class using an identifier of the same sort you've been using for variables. By convention though, class names in Java begin with a capital letter so our class name is Sphere with a capital S. If you adopt this approach, you will be consistent with most of the code you come across. You could enter this sourcecode and save it as the file We will be building on this class, and using it in a working example, a little later in this chapter.

The keyword static in the first line of the definition specifies the variable PI as a class variable rather than an instance variable. The variable PI is also initialized with the value 3.14. The keyword final tells the compiler that you do not want the value of this variable to be changed, so the compiler will check that this variable is not modified anywhere in your program. Obviously this is a very poor value for p. You would normally use Math.PI - which is defined to twenty decimal places, close enough for most purposes.


Whenever you want to fix the value stored in a variable, that is, make it a constant, you just need to declare the variable with the keyword final and specify its initial value. By convention, constants have names in capital letters.

The next variable, count, is also declared with the keyword static. All objects of the Sphere class will share one copy of count, and one of PI. We have initialized the variable count to 0, but since it is not declared with the keyword final, we can change its value.

The next four variables in the class definition are instance variables, as they don't have the keyword static applied to them. Each object of the class will have its own separate set of these variables storing the radius and the coordinates of the center of the sphere. Although we haven't put initial values for these variables here, we could do so if we wanted. If you don't specify an initial value, a default value will be assigned automatically when the object is created. Fields that are of numeric types will be initialized with zero, fields of type char will be initialized with '\u000', and fields that store class references or references to arrays will be initialized with null.

There has to be something missing from the definition of the Sphere class - there is no way to set the value of radius and the other instance variables once a particular Sphere object is created. There is nothing to update the value of count either. Adding these things to the class definition involves using methods, so we now need to look at how a method is put together.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor