Static class Example

Static class Example

Previous Home Next

 

Following program for calculate Area and circumference of a Rectangle using static class.

 

 
/*
 * Save as a Static.java
 * program for calculate Area and circumference of a Rectangle using static class.
 * Class is static, it make only one copy into memory refrence.
 */
package r4r.co.in;

// Initilized the Staic parameter,
 
 public class Static {

    static int length = 10;
    static int breath = 20;

    static {
        
  // Call the myClass() method into static class.
        myClass();
    }

    private static void myClass() {

        System.out.println("Length of Rectangle =" + length);
        System.out.println("Breath of Rectangle =" + breath);

   //Area of rectangle= length of rectangle X breath of rectangle
        float a = (float) (length * breath);
        System.out.println("Area of Rectangle:" + a);

 // Circumference of rectangle=2x (length of rectangle + breath of rectangle)
         float b = (float) (2 *( length + breath));
         System.out.println("Circumference of a rectangle:" + b);
    }

    public static void main(String[] args) {

    }
}

Length of Rectangle =10
Breath of Rectangle =20
Area of Rectangle:200.0
Circumference of a rectangle:60.0
Previous Home Next