The class is at the core of the java. It is the logical construct upon which the entire java language is built because it defines the shape and nature of an object or we can say, the class forms the basis for OOP (Object Oriented Programming) in java. Any concept that you wish to implement in a Java program must be encapsulated within a class.java classes

General form of a Class –

When you define a class, you declare its exact form and nature. You do this by specifying the data that it contains and the code that operates on that data.

A simplified general form of a class is shown here – 

Class classname {

type instance-variable 1 ;

type instance-variable 2 ;

// . . . . .

type instance-variable N ;

type methodname1(parameter-list) {

// body of method

}

type methodname2(parameter-list) {

// body of method

}

// . . . . .

typemethodname N(parameter-list) {

// body of method

}

}

A Simple Class Program –

/* A program that uses the Abc class.

Call this file AbcDemo.java

*/

class  Abc {

double width ;

double height ;

double depth ;

}

// This class declares an object of type Abc .

class AbcDemo {

public static void main(String args[] ) {

Abc myabc=new Abc() ;

double vol ;

// assign values to myabc’s instance variables

myabc.width= 10 ;

myabc.height= 20 ;

myabc.depth= 15 ;

// compute volume of abc

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

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

}

}

You should call the file that contains this program AbcDemo.java, because the main() method is in the class called AbcDemo, not the class called Abc. When you compile this program, you will find that two .class files have been created, one for Abc and one for AbcDemo. The Java compiler automatically puts each class into its own .class file.

Recommended Post   Overview of Java

It is not necessary for both the Abcand AbcDemo class to actually be in the same source file. You could put each class in its own file, called Abc.javaand AbcDemo.java, respectively.

To run this program, you, must execute AbcDemo.class . When you do, you will see the following Output:-

Volume is 3000.0