Press "Enter" to skip to content

Java – Class Fundamentals and Forms of Class

In this tutorial, you will learn about Java class fundamentals and forms of class.

Class Fundamentals

The most crucial thing to understand about a class is that it defines a new data type. Once described, this new type can be employed to form objects of that type. Hence, a class is a template for an object, and an object is an instance of a class. As an object is an instance of a class, you will normally notice the two words object and instance used interchangeably.

The General Forms of a Class

When you specify a class, you declare its specific type and nature. You do this by particularizing the data that it carries and the code that operates on that data. While very simple classes may include only code or only data, most real-world classes contain both. As you will notice, a class code defines the interface to its data.

A class is declared by class keyword. Classes can get much more complicated. A clear general form of a class definition is given 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 in a class are called instance variables. The code is enclosed inside methods. Collectively, the methods and variables defined inside a class are known as members of the class. In most classes, the instance variables are worked upon and accessed by the methods defined for that class. Thus, as a common rule, it is the methods that determine how a class’ data can be used.

Variables specified in a class are called instance variables because each instance of the class has its own copy of these variables. Hence, the data for one object is separate and different from the data for another.

All methods have the same command form as main( ). However, most methods will not be defined as static or public. Mark that the general form of a class does not specify a main( ) method. Java classes do not require to have a main( ) method. You only define one if that class is the starting point for your program. Moreover, some kinds of Java applications, such as applets, don’t need a main( ) method at all.

A Simple Class

Let’s start our study of the class with a simple illustration. Here is a class called Box that illustrates three instance variables: width, height, and depth.

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

As said, a class defines a new type of data. In this example, the new data type is known as Box. You will employ this name to declare objects of type Box. It is necessary to remember that a class declaration only creates a template; it does not create an actual object. Thus, the above code does not cause any objects of type Box to come into existence.

To 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. Thus, it will have “physical” reality.

To assign the width variable of mybox the value 100, you would use the following statement:

mybox.width = 100;

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

Here is a comprehensive 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 includes this program BoxDemo.java, because of the main( ) method is in the class called BoxDemo.

When you compile this program, you will find that two .class files have been created, one for Box and one for BoxDemo. It is not compulsory for both the Box and the BoxDemo class to be in the same source file. You could put every 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 result:

Volume is 3000.0

As said earlier, each object has its own copies of the instance variables. This indicates that if you have two Box objects, each has its own copy of depth, width, and height. It is vital to understand that changes to the instance variables of one object have no impact on the instance variables of another. For instance, 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 given here:

Volume is 3000.0
Volume is 162.0

As you can see, mybox1’s data is entirely separate from the data contained in mybox2.