Like Instance inner class we have also static inner class. Static instance class is loaded at the time of loading the outer class as it is declared as static that means it will act as the static member of the outer class and hence we can be able to define static member as it get initialized at the time of loading the outer class.

Important point regarding Static inner class

1. We can be able to define static and instance both in the static inner class.

2. We can have main() method which will make static inner class possible to have starting point for any java program.

3.  It can be accessed from both static and instance scope of outer class.

4. ' this ' will always refer to the outer class member , to access inner class member we need to create inner class object.

5. As it a static class then we can be able to access the static member with the class name itself. Object will be needed to access instance member.

6. Inner class is extending Object class that means we cannot use outer class member with the help of inner class object.

Example showing the working of an static inner class

public class A{

   int x;        // instance member of outer class

static class B{     // inner static class

   B(){           // inner class constructor 

        System.out.println("Inner class constructor");
}

   int x;      // inner class instance member

   static int z=40;   // inner class static member

}


A(){                    // outer class constructor

    A.B a = new A.B();   // inner class object 

    a.x=30;

   System.out.println(a.x); 

   System.out.println(this.x);

   System.out.println(A.B.z); // Accessing inner class static 
                                with class name 

}

public static void main(String[] args) {

A p = new A();

A.B b = new A.B();     // inner class in static method 

Class c = b.getClass(); //getting class name of inner class

System.out.println(c.getName());

c=c.getSuperclass();  //getting superclass name of inner class

System.out.println(c.getName());

     }
}

Related Posts:

  • Inner Class in Java Inner class in a class defined as a class inside another class. So it is itself a class but first it will act as a member of that class. As we know that we cannot make a class as static but inner class can be made as static… Read More
  • Type of inheritance in Java According to OOPS ( Object-oriented concept ) inheritance are classified into 6 category in which java support 5 of them and one is not supported in java i.e multiple inheritance for which there is a concept of interface . I… Read More
  • Constructor in Java Constructor is the member of the class and it has name same as class name. Constructor is the one of the important concept in java which is responsible to initialize the instance member of the class. Its functionality is lik… Read More
  • super keyword in Java Like this in java one more most important keyword is there i.e super. super in Java has a wide use and it is mainly used to call the super class constructor explicitly by the user and implicitly used by the JVM to initialize… Read More
  • ' this ' keyword in Java this is one of the most important keyword in java which refers to the current class object. Like we are creating an object for our class similarly internally a object is there called as this which points to the current class… Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201977

Online Members

Live Traffic