Java Programing laungage

What are different types of classes in java ?
Previous Home Next

Java possesses five different types of class constructs and it is good to know where to use each type. The various class types used in java are the following :

  1. Top Level Classes : A top level class is the one that is present on the top in the hierarchy of class design as without it one can not develop software at all. An example of top level class is given as,

    
    Java Tutorialsclass r4r // top level class
    {
     public static void main(String args[])
      {
         // some logic
      }
    }
    
    
  2. Static Inner Classes : Static member classes are the most common type of nested classes. A nested class is defined inside the body of an enclosing class and should only be present to assist the enclosing class.

    There is no boundation to the depth of nesting of classes but it is quite very rare to see more than one level of such classes. Static member classes are also know as static inner classes.The syntax of static member classes is shown in following code snippet,

    Example

    Java Tutorialsclass InnerStaticClassExample 
    // main class
    {
    public static void main(String args[])
    {
    shape.Rectangle obj=new shape.Rectangle(4,7);             
    // creating object for inner class Rectangle, using dot notation
    obj.area();                                                                      
    //calculating the area of Rectangle
    System.out.println("The value of x is: "+t.a);                    
    //printing the variable defined in Rectangle     
    obj.new Java Tutorialsircle(7).area();                                               
    //calculating the area of circle using the object of Rectangle
    System.out.println("The value of y is :"+obj.new Java Tutorialsircle(7).y);  
    //printing the value of y
    }
    Java Tutorialslass Shape                                                       
    	//top level class
    {
    public static class Rectangle                                
    	//static-inner class, whose enclosing class is Shape
    {
    int a=10;
    int length;
    int breadth;
    public Rectangle(int length, int breadth)                  
    	//constructor of Rectangle
    {
    this.length=length;
    this.breadth=breadth;
    }
    public void area()                                         
    	//this method calculates the area of rectangle
    {
    System.out.println("The area of rectangle is :"+length*breadth);
    }
    public class Java Tutorialsircle                                      
    // simple member class Circle, whose enclosing class is Rectangle which is static 
    {
    int radius;
    int y=27;
    public Java Tutorialsircle(int radius)                             
    	//  constructor of Circle
    {
    this.radius=radius;
    }
    public void area()
    {
    System.out.println("The area of circle is: "+Math.PI*radius*radius);
    }
    }
    }
    }
    }
    
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: