Java Classes

Java Classes

Introducing Classes

 

The class is at the core of Java. The class forms the basis for object-oriented programming in Java. Any concept you wish to implement in a Java program must be encapsulated within a class.

 

Class Fundamentals

The classes created in the preceding chapters primarily exist simply to encapsulate the main( ) method, which has been used to demonstrate the basics of the Java syntax. Perhaps the most important thing to understand about a class is that it defines a new data type. Once defined, this new type can be used to create objects of that type. Thus, a class is a template for an object, and an object is an instance of a class.

The General Form of a Class

When defining a class, declare its exact form and nature. This is done by specifying the data that it contains and the code that operates on that data. While very simple classes may contain only code or only data, most real-world classes contain both. A class is declared by use of the class keyword. The general form of a class definition is shown here:

 

class classname {

type instance-variable1;

type instance-variable2;

// …

type instance-variableN;

type methodname1(parameter-list) {

// body of method

}

type methodname2(parameter-list) {

// body of method

}

// …

type methodnameN(parameter-list) {

// body of method

}

}

 

The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. In most classes, the instance variables are acted upon and accessed by the methods defined for that class. Thus, it is the methods that determine how a class’ data can be used. Variables defined within a class are called instance variables because each instance of the class that is, each object of the class contains its own copy of these variables. Java classes do not need to have a main( ) method. You only specify one if that class is the starting point for your program.

A Simple Class

Here is a class called Box that defines three instance variables: width, height, and depth. Currently, Box does not contain any methods.

 

class Box {

double width;

double height;

double depth;

}

 

a class defines a new type of data. In this case, the new data type is called Box. You will use this name to declare objects of type Box. It is important to remember that a class declaration only creates a template; it does not create an actual object. Thus, the preceding code does not cause any objects of type Box to come into existence. To actually create a Box object, you will use a statement like the following:

Box mybox = new Box(); // create a Box object called mybox

 

After this statement executes, mybox will be an instance of Box. Again, each time you create an instance of a class, you are creating an object that contains its own copy of each instance variable defined by the class. Thus, every Box object will contain its own copies of the instance variables width, height, and depth. To access these variables, you will use the dot (.) operator. The dot operator links the name of the object with the name of an instance variable. For example, to assign the width variable of mybox the value 100, you would use the following statement:

 

mybox.width = 100;

 

This statement tells the compiler to assign the copy of width that is contained within the mybox object the value of 100. In general, you use the dot operator to access both the instance variables and the methods within an object. Here is a complete program that uses the Box class:

 

/* A program that uses the Box class.

Call this file BoxDemo.java

*/

class Box {

double width;

double height;

double depth;

}

// This class declares an object of type Box.

class BoxDemo {

public static void main(String args[]) {

Box mybox = new Box();

double vol;

// assign values to mybox’s instance variables

mybox.width = 10;

mybox.height = 20;

mybox.depth = 15;

// compute volume of box

vol = mybox.width * mybox.height * mybox.depth;

System.out.println(“Volume is ” + vol);

}

}

 

You should call the file that contains this program BoxDemo.java, because the main( ) method is in the class called BoxDemo, not the class called Box. When you compile this program, you will find that two .class files have been created, one for Box and one for BoxDemo. The Java compiler automatically puts each class into its own .class file. It is not necessary for both the Box and the BoxDemo class to actually be in the same source file. You could put each class in its own file, called Box.java and BoxDemo.java, respectively. To run this program, you must execute BoxDemo.class. When you do, you will see the following output:

 

Volume is 3000.0

 

As stated earlier, each object has its own copies of the instance variables. This means that if you have two Box objects, each has its own copy of depth, width, and height. It is important to understand that changes to the instance variables of one object have no effect on the instance variables of another. For example, the following program declares two Box objects:

 

// This program declares two Box objects.

class Box {

double width;

double height;

double depth;

}

class BoxDemo2 {

public static void main(String args[]) {

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

// assign values to mybox1’s instance variables

mybox1.width = 10;

mybox1.height = 20;

mybox1.depth = 15;

/* assign different values to mybox2’s

instance variables */

mybox2.width = 3;

mybox2.height = 6;

mybox2.depth = 9;

// compute volume of first box

vol = mybox1.width * mybox1.height * mybox1.depth;

System.out.println(“Volume is ” + vol);

// compute volume of second box

vol = mybox2.width * mybox2.height * mybox2.depth;

System.out.println(“Volume is ” + vol);

}

}

 

The output produced by this program is shown here:

Volume is 3000.0

Volume is 162.0

 

Declaring Objects

As just explained, when you create a class, you are creating a new data type. You can use this type to declare objects of that type. However, obtaining objects of a class is a two-step process. First, you must declare a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. The new operator dynamically allocates that is, allocates at run time memory for an object and returns a reference to it. This reference is, more or less, the address in memory of the object allocated by new. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated. the following is used to declare an object of type Box:

 

Box mybox = new Box();

 

This statement combines the two steps just described. It can be rewritten like this to show each step more clearly:

 

Box mybox; // declare reference to object

mybox = new Box(); // allocate a Box object

 

The first line declares mybox as a reference to an object of type Box. After this line executes, mybox contains the value null, which indicates that it does not yet point to an actual object. Any attempt to use mybox at this point will result in a compile-time error. The next line allocates an actual object and assigns a reference to it to mybox. After the second line executes, you can use mybox as if it were a Box object. But in reality, mybox simply holds the memory address of the actual Box object. The effect of these two lines of code is depicted in Figure 6-1.

 

A Closer Look at new

 

As just explained, the new operator dynamically allocates memory for an object. It has this general form:

 

class-var = new classname( );

 

Here, class-var is a variable of the class type being created. The classname is the name of the class that is being instantiated. The class name followed by parentheses specifies the constructor for the class. A constructor defines what occurs when an object of a class is created. However, if no explicit constructor is specified, then Java will automatically supply a default constructor. This is the case with Box. It is important to understand that new allocates memory for an object during run time. The advantage of this approach is that your program can create as many objects as it needs during the execution of your program.  A class creates a new data type that can be used to create objects. That is, a class creates a logical framework that defines the relationship between its members. When you declare an object of a class, you are creating an instance of that class. Thus, a class is a logical construct. An object has physical reality and an object occupies space in memory.

Assigning Object Reference Variables

For example:

 

Box b1 = new Box();

Box b2 = b1;

 

after this executes, b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object through b2 will affect the object to which b1 is referring, since they are the same object.  Look at the other example:

 

Box b1 = new Box();

Box b2 = b1;

// …

b1 = null;

 

Here, b1 has been set to null, but b2 still points to the original object. When you assign one object reference variable to another object reference variable, you are not creating a copy of the object, you are only making a copy of the reference.

 

Introducing Methods

 

Classes usually consist of two things: instance variables and methods. This is the general form of a method:

 

type name(parameterlist) {

// body of method

}

 

Here, type specifies the type of data returned by the method. This can be any valid type, including class types that you create. If the method does not return a value, its return type must be void. The name of the method is specified by name. This can be any legal identifier other than those already used by other items. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called. If the method has no parameters, then the parameter list will be empty. Methods that have a return type other than void return a value to the calling method using the following form of the return statement:

 

return value;

 

Here, value is the value returned.

Adding a Method to the Box Class

 

Although it is perfectly fine to create a class that contains only data. Most of the time you will use methods to access the instance variables defined by the class. In addition to defining methods that provide access to data, you can also define methods that are used internally by the class itself. Let’s begin by adding a method to the Box class. since the volume of a box is dependent upon the size of the box, it makes sense to have the Box class compute it. To do this, you must add a method to Box, as shown here:

 

// This program includes a method inside the box class.

class Box {

double width;

double height;

double depth;

// display volume of a box

void volume() {

System.out.print(“Volume is “);

System.out.println(width * height * depth);

}

}

class BoxDemo3 {

public static void main(String args[]) {

Box mybox1 = new Box();

Box mybox2 = new Box();

// assign values to mybox1’s instance variables

mybox1.width = 10;

mybox1.height = 20;

mybox1.depth = 15;

/* assign different values to mybox2’s

instance variables */

mybox2.width = 3;

mybox2.height = 6;

mybox2.depth = 9;

// display volume of first box

mybox1.volume();

// display volume of second box

mybox2.volume();

}

}

 

This program generates the following output, which is the same as the previous version.

Volume is 3000.0

Volume is 162.0

 

Look closely at the following two lines of code:

mybox1.volume();

mybox2.volume();

 

The first line here invokes the volume( ) method on mybox1. That is, it calls volume( ) relative to the mybox1 object, using the object’s name followed by the dot operator. Thus, the call to mybox1.volume( ) displays the volume of the box defined by mybox1, and the call to mybox2.volume( ) displays the volume of the box defined by mybox2. Each time volume( ) is invoked, it displays the volume for the specified box. After the statements inside volume( ) have executed, control is returned to the calling routine, and execution resumes with the line of code following the call. There is something very important to notice inside the volume( ) method: the instance variables width, height, and depth are referred to directly, without preceding them with an object name or the dot operator. When a method uses an instance variable that is defined by its class, it does so directly, without explicit reference to an object and without use of the dot operator.  A method is always invoked relative to some object of its class. Once this invocation has occurred, the object is known. This means that width, height, and depth inside volume( ) implicitly refer to the copies of those variables found in the object that invokes volume( ).

Returning a Value

 

While the implementation of volume( ) does move the computation of a box’s volume inside the Box class where it belongs, it is not the best way to do it. A better way to implement volume( ) is to have it compute the volume of the box and return the result to the caller. The following example does just that:

// Now, volume() returns the volume of a box.

class Box {

double width;

double height;

double depth;

// compute and return volume

double volume() {

return width * height * depth;

}

}

class BoxDemo4 {

public static void main(String args[]) {

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

// assign values to mybox1’s instance variables

mybox1.width = 10;

mybox1.height = 20;

mybox1.depth = 15;

/* assign different values to mybox2’s

instance variables */

mybox2.width = 3;

mybox2.height = 6;

mybox2.depth = 9;

// get volume of first box

vol = mybox1.volume();

System.out.println(“Volume is ” + vol);

// get volume of second box

vol = mybox2.volume();

System.out.println(“Volume is ” + vol);

}

}

 

As you can see, when volume( ) is called, it is put on the right side of an assignment statement. On the left is a variable, in this case vol, that will receive the value returned by volume( ). Thus, after

 

vol = mybox1.volume();

 

executes, the value of mybox1.volume( ) is 3,000 and this value then is stored in vol. There are two important things to understand about returning values:

 

  • The type of data returned by a method must be compatible with the return type specified by the method. For example, if the return type of some method is boolean, you could not return an integer.
  • The variable receiving the value returned by a method such as vol, in this case must also be compatible with the return type specified for the method.

Adding a Method That Takes Parameters

 

Parameters allow a method to be generalized. That is, a parameterized method can operate on a variety of data and/or be used in a number of slightly different situations. Here is a method that returns the square of the number 10:

 

int square()

{

return 10 * 10;

}

 

While this method return the value of 10 squared. However, if you modify the method so that it takes a parameter, as shown next, then you can make square( ) much more useful.

int square(int i)

{

return i * i;

}

 

Now, square( ) will return the square of whatever value it is called with. That is, square( ) is now a general-purpose method that can compute the square of any integer value, rather than just 10. Here is an example:

 

int x, y;

x = square(5); // x equals 25

x = square(9); // x equals 81

y = 2;

x = square(y); // x equals 4

 

In the first call to square( ), the value 5 will be passed into parameter i. In the second call, i will receive the value 9. The third invocation passes the value of y, which is 2 in this example. As these examples show, square( ) is able to return the square of whatever data it is passed. It is important to keep the two terms parameter and argument straight. A parameter is a variable defined by a method that receives a value when the method is called. For example, in square( ), i is a parameter. An argument is a value that is passed to a method when it is invoked. For example, square(100) passes 100 as an argument. Inside square( ), the parameter i receives that value. Thus, a better approach to setting the dimensions of a box is to create a method that takes the dimension of a box in its parameters and sets each instance variable appropriately. This concept is implemented by the following program:

 

// This program uses a parameterized method.

class Box {

double width;

double height;

double depth;

// compute and return volume

double volume() {

return width * height * depth;

}

// sets dimensions of box

void setDim(double w, double h, double d) {

width = w;

height = h;

depth = d;

}

}

class BoxDemo5 {

public static void main(String args[]) {

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

// initialize each box

mybox1.setDim(10, 20, 15);

mybox2.setDim(3, 6, 9);

// get volume of first box

vol = mybox1.volume();

System.out.println(“Volume is ” + vol);

// get volume of second box

vol = mybox2.volume();

System.out.println(“Volume is ” + vol);

}

}

 

As you can see, the setDim( ) method is used to set the dimensions of each box. For example, when mybox1.setDim(10, 20, 15); is executed, 10 is copied into parameter w, 20 is copied into h, and 15 is copied into d. Inside setDim( ) the values of w, h, and d are then assigned to width, height, and depth, respectively.

 

Constructors

 

Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor. A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. Constructors have no return type, not even void. This is because its is called implicitly or automatically. You can rework the Box example so that the dimensions of a box are automatically initialized when an object is constructed. This version shows constructors:

 

/* Here, Box uses a constructor to initialize the

dimensions of a box.

*/

class Box {

double width;

double height;

double depth;

// This is the constructor for Box.

Box() {

System.out.println(“Constructing Box”);

width = 10;

height = 10;

depth = 10;

}

// compute and return volume

double volume() {

return width * height * depth;

}

}

class BoxDemo6 {

public static void main(String args[]) {

// declare, allocate, and initialize Box objects

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

// get volume of first box

vol = mybox1.volume();

System.out.println(“Volume is ” + vol);

// get volume of second box

vol = mybox2.volume();

System.out.println(“Volume is ” + vol);

}

}

 

When this program is run, it generates the following results:

Constructing Box

Constructing Box

Volume is 1000.0

Volume is 1000.0

 

As you can see, both mybox1 and mybox2 were initialized by the Box( ) constructor when they were created. Since the constructor gives all boxes the same dimensions, 10 by 10 by 10, both mybox1 and mybox2 will have the same volume.  As you know, when you allocate an object, you use the following general form:

 

class-var = new classname( );

 

Now you can understand why the parentheses are needed after the class name. What is actually happening is that the constructor for the class is being called. Thus, in the line

 

Box mybox1 = new Box();

 

new Box( ) is calling the Box( ) constructor. When you do not explicitly define a constructor for a class, then Java creates a default constructor for the class. This is why the preceding line of code worked in earlier versions of Box that did not define a constructor. The default constructor automatically initializes all instance variables to zero. Once you define your own constructor, the default constructor is no longer used.

 

Parameterized Constructors

 

While the Box( ) constructor in the preceding example does initialize a Box object, it is not very useful since all boxes have the same dimensions. What is needed is a way to construct Box objects of various dimensions. The easy solution is to add parameters to the constructor. For example, the following version of Box defines a parameterized constructor which sets the dimensions of a box as specified by those parameters. For example:

 

/* Here, Box uses a parameterized constructor to

initialize the dimensions of a box.

*/

class Box {

double width;

double height;

double depth;

// This is the constructor for Box.

Box(double w, double h, double d) {

width = w;

height = h;

depth = d;

}

// compute and return volume

double volume() {

return width * height * depth;

}

}

class BoxDemo7 {

public static void main(String args[]) {

// declare, allocate, and initialize Box objects

Box mybox1 = new Box(10, 20, 15);

Box mybox2 = new Box(3, 6, 9);

double vol;

// get volume of first box

vol = mybox1.volume();

System.out.println(“Volume is ” + vol);

// get volume of second box

vol = mybox2.volume();

System.out.println(“Volume is ” + vol);

}

}

The output from this program is shown here:

Volume is 3000.0

Volume is 162.0

 

As you can see, each object is initialized as specified in the parameters to its constructor. For example, in the following line,

 

Box mybox1 = new Box(10, 20, 15);

 

the values 10, 20, and 15 are passed to the Box( ) constructor when new creates the object. Thus, mybox1’s copy of width, height, and depth will contain the values 10, 20, and 15, respectively.

 

The this Keyword

 

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class’ type is permitted. consider the following version of Box( ):

 

// A redundant use of this.

Box(double w, double h, double d) {

this.width = w;

this.height = h;

this.depth = d;

}

 

This version of Box( ) operates exactly like the earlier version. The use of this is redundant, but perfectly correct. Inside Box( ), this will always refer to the invoking object.

Instance Variable Hiding

 

you can have local variables, including parameters to methods, which overlap with the names of the class’ instance variables. However, when a local variable has the same name as an instance variable, the local variable hides the instance variable. This is why width, height, and depth were not used as the names of the parameters to the Box( ) constructor inside the Box class. Because this lets you refer directly to the object, you can use it to resolve any name space collisions that might occur between instance variables and local variables. For example, here is another version of Box( ), which uses width, height, and depth for parameter names and then uses this to access the instance variables by the same name:

 

// Use this to resolve name-space collisions.

Box(double width, double height, double depth) {

this.width = width;

this.height = height;

this.depth = depth;

}

 

Garbage Collection

 

Since objects are dynamically allocated by using the new operator, you might be wondering how such objects are destroyed and their memory released for later reallocation. Java handles deallocation automatically. The technique that accomplishes this is called garbage collection.  when no references to an object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed. Garbage collection only occurs periodically during the execution of your program.

 

The finalize( ) Method

 

Sometimes an object will need to perform some action when it is destroyed. For example, if an object is holding resource such as a file, then you might want to make sure these resources are freed before an object is destroyed. To handle such situations, Java provides a mechanism called finalization. By using finalization, you can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector. To add a finalizer to a class, you simply define the finalize( ) method. The Java run time calls that method whenever it is about to recycle an object of that class. Inside the finalize( ) method you will specify those actions that must be performed before an object is destroyed. The garbage collector runs periodically, checking for objects that are no longer referenced by any running state. The finalize( ) method has this general form:

 

protected void finalize( )

{

// finalization code here

}

 

Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class. It is important to understand that finalize( ) is only called just prior to garbage collection. It is not called when an object goes out-of-scope. Java does not support this idea of destructors. The finalize( ) method only approximates the function of a destructor.