The following code shows an Example of Creating a Class in Java.

A simple example of creating a class in Java is presented here. Class Box has three instance variables, a constructor, and two methods – show(), and volume().

class Box
    double width, height, depth;
    public Box(double width, double height, double depth)
    public double volume()
        return width*height*depth;
    public void show()
        System.out.println("Width: "+width);
        System.out.println("Height: "+height);
        System.out.println("Depth: "+depth);
public class Main
	public static void main(String[] args) {
	    System.out.println("Creating Box Object...");
	    Box ob=new Box(45, 12.9, 12.6);;
	    System.out.println("Box Volume: "+ob.volume());	    


Creating Box Object…
Width: 45.0
Height: 12.9
Depth: 12.6
Box Volume: 7314.3

Further Reading

Java Practice Exercise