Java Reference
In-Depth Information
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 that I 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 the 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
source code and save it as the file Sphere.java . You add to this class definition and use it in a working
example a little later in this chapter.
You may have noticed that in the examples in previous chapters the keyword public in this context pre-
ceded the keyword class in the first line of the class definition. The effect of the keyword public is bound
up with the notion of a package containing classes, but I'll defer discussing this until a little later in this
chapter when you have a better idea of what makes up a class definition.
The keyword static in the first line of the Sphere class 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 checks
that this variable is not modified anywhere in your program. Obviously, this is a very poor value for π. You
would normally use Math.PI — which is defined to 20 decimal places, close enough for most purposes.
NOTE Whenever you want to make sure that a variable will not be modified, you just need to
declarethevariablewiththekeyword final . Byconvention,variablesthatareconstantshave
names in capital letters.
You have also declared the next variable, count , using the keyword static . All objects of the Sphere
class have access to and share the one copy of count and the one copy of PI that exist. You have initialized
the variable count to 0, but because you have not declared it using the keyword final , you 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 has its own separate set of these variables, storing the radius
and the coordinates of the center of the sphere. Although you haven't put initial values for these variables
here, you can do so if you want. If you don't specify an initial value, a default value is assigned automat-
Search WWH ::




Custom Search