Java Class

The most important thing to understand about class is that it defines a new data type. Once defined, this new type can be used to create objects of that type. Therefore, a class is a template for an object, and an object is an instance of the class because an object is an instance of a class, you will often see the two words, object and instance used interchangeably.

When you define a class in Java, you declare its exact form and nature. You do this by specifying the data that it contains and the code that works on that data. While very simple classes may contain only code or only data, most real-world classes have both. As you will see, a class code defines the interface to its data.

Java class Keyword

A class is declared by the use of the class keyword.

The classes that have been used up to this point are actually very limited illustrations of its complete form. Classes can (and usually do) get much more complex.

A simplified form of a class definition is shown below :

class classname
{
   type instance-variable1;
   type instance-variable2;
   
   //...
   
   type instance-variableN;
   
   type methodname1(parameter-list)
   {
      // body of the method
   }
   type methodname2(parameter-list)
   {
      // body of the method
   }
   
   //...
   
   type methodnameN(parameter-list)
   {
      // body of the method
   }
}

The data, or the variables defined within a class are called instance variables.

The code is obtained within the methods. Collectively, the methods and the variables defined inside a class are called members of the class.

In most classes, the instance variables are acted upon and accessed through the methods defined for that class. Thus, as a general rule, it is methods that determine how a class data can be used.

Java Instance Variables

Variables defined within a class are called instance variables because all instance of the class (i.e., all object of the class) contains its own copy of these variables. Therefore, the data for one object is unique and separate from the data for another.

All methods have same general form as main(), which we have been using so far. Nevertheless, most methods will not be specified a static or public.

Notice that the common form of a class does not specify a main() method. Java classes do not need to have a main() method. You only define one if that class is the starting point for your program. Further, some kinds of Java applications, such as applets, don't require a main() method at all.

Simple Java Class

Now let's start our study of a class with a simple example. Here is a class called Box that defines these three instance variables:

Currently, Box does not have any methods (but some will be added soon).

class Box
{
   double width;
   double height;
   double depth;
}

As stated, a class defines a new data type called Box in this case. You will use this name to declare objects of type Box.

It is significant to remember that a class declaration only creates a template, it does not create an actual object. Therefore, 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 as shown here:

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

After above statement executes, mybox will be an instance of Box. Thus, it will have "physical" reality. For the moment, don't worry about the details of above statement.

As discussed earlier, each time you create an instance of a class, you are creating an object which contains its own copy of each instance variable defined by the class. Therefore, every Box object will also contain its own copies of the instance variables width, height, and depth. You will use the dot operator (.) to access these variables.

The dot operator (.) links the name of the object with the name of an instance variable. For instance, to assign the width variable of mybox, the value 100, you would use the following statement :

mybox.width = 100;

Above statement tells the compiler to assign the copy of width that is contained within the mybox object the value of 100.

In common, you use the dot operator to access both the instance variables and the methods within an object. One another point: Even though commonly referred to as the dot operator, the formal specification for Java categorizes the . as a separator. Nevertheless, as the use of the term "dot operator" is widespread.

Java class Example

Following is a complete program that uses Box class :

/* Java Program Example - Java Classes
*  Call this file BoxDemo.java */

class Box
{
    double width;
    double height;
    double depth;
}

/* class that declares an object of type Box */
class BoxDemo
{
    public static void main(String args[])
    {
        Box mybox = new Box();
        double vol;
        
        /* assign the values to the mybox's instance variables */
        mybox.width = 100;
        mybox.height = 200;
        mybox.depth = 150;
        
        /* now compute the volume of the 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, since the main() method is in the class called BoxDemo, not the class called Box.

When you compile this program, you will find that the two .class files have been created, one for the Box and one for the BoxDemo class. The Java compiler automatically places each class into its own .class file. It is not necessary for both Box and BoxDemo to actually exist in the same source file. You could put each class in its own file, named Box.java and BoxDemo.java, respectively.

To run above program, you must execute BoxDemo.class. When you do this, you will see the following output:

java classes

As told earlier, each object has its own copies of the instance variables. This means that if you have two Box objects, each having its own copy of , and height, width, and depth.

It is important to understand that changes to the instance variables of one object have none effect on the instance variables of another. For instance, the following program declares the two Box objects :

/* Java Program Example - Java Classes
*  This program declares two Box objects */

class Box
{
    double width;
    double height;
    double depth;
}

class BoxDemo
{
    public static void main(String args[])
    {
        
        Box mybox1 = new Box();
        Box mybox2 = new Box();
        double vol;
        
        /* assign the values to the mybox1's instance variables */
        mybox1.width = 100;
        mybox1.height = 200;
        mybox1.depth = 150;
        
        /* assign different values to the mybox2's instance variables */
        mybox2.width = 30;
        mybox2.height = 60;
        mybox2.depth = 90;
        
        /* compute the volume of the first box */
        vol = mybox1.width * mybox1.height * mybox1.depth;
        
        /* print the volume of the first box */
        System.out.println("Volume of first box is " + vol);
        
        /* compute the volume of the second box */
        vol = mybox2.width * mybox2.height * mybox2.depth;
        
        /* print the volume of the second box */
        System.out.println("Volume of second box is " + vol);
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java classes example

As you can see, from the above output, mybox1's data is completely separate from the data contained in mybox2.

Java Online Test


« Previous Tutorial Next Tutorial »