Java-Simple Class Construction

Simple Class Construction

The class is nothing but a template for object you are going to create or it’s a blue print by using this we create an object. In simple word we can say it’s a specification or a pattern which we define and every object we define will follow that pattern. Here is a template for the source code of a class :

The first piece is a class-header which consists of the keyword “class” and the “name” you give to the class. Names in programming languages are also known as identifiers. The second piece is the body.  It consists of a pair of open/close squiggly brackets with stuff in between them. For example :

Here is a class called Area that defines three instance variables : length, height, and width. A class defines a new type of data. In this case, the new data type is called Area. You will use this name to declare objects of type Area. 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 Area to come into existence. To actually create a Area object, you will use a statement like the following :

After this statement executes,’a1‘ will be an instance of Area. 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 Area object will contain its own copies of the instance variables length, height, and width. 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 length variable of Area the value 100, you would use the following statement :

This statement tells the compiler to assign the copy of length that is contained within the ‘a1’ object the value of 100. In general, you use the dot operator to access the instance variables within an object.


You should call the file that contains this program, because the main( ) method is in the class called Javaapp, not the class called Area. When you compile this program, you will find that two .class files have been created, one for Area and one for Javaapp. The Java compiler automatically puts each class into its own .class file. It is not necessary for both the Area and the Javaapp class to actually be in the same source file. You could put each class in its own file, called and, respectively.

Program Source

class Area{
    int length;
    int height;
    int width;

public class Javaapp {

    public static void main(String[] args) {
        Area a1 = new Area();
        a1.length = 100;
        a1.height  = 25;
        a1.width  = 75;
        System.out.println("Area a1 length = "+a1.length);
        System.out.println("Area a1 height  = "+a1.height);
        System.out.println("Area a1 width  = "+a1.width);
        int volume = a1.length*a1.height*a1.width;
        System.out.println("Area a1 volume is : "+volume);

Leave a Comment