Difference between static and non-static java inner class.

A static java inner class cannot have instances. A non-static java inner class can have instances that belong to the outer class.

This Core Java tutorial was added on 12/05/2008.

Comments on "Difference between static and non-static java inner class." Tutorial:

  1. james vinett says:

    “A static java inner class cannot have instances.”

    I’ve seen this written before, but it cannot be true. You can, in fact, call “new” on a static nested class and therefore have an instance. My understanding of a static nested class is that it has exactly the same behavior as an outer class. Nesting in this case is a design choice.

    Would you, please, explain to me, if I am incorrect, what it is exactly that you mean by your statement. Thanks.

    Btw: I really enjoy your site.

  2. ashish says:

    A static inner class can have instances but they dont have enclosing instances! which a non-static inner class have.

    eg.

    class A{

    static class B{} // static-member class
    class C{} // non-static member class
    }

    class D{
    public static void main(String st[]){

    A obj1 = new A();
    A.C = obj1.new C();//obj1 is enclosing instance
    A.B = new A.B(); //No enclosing instance requird

    }
    }

  3. W. R. Shastry says:

    There is two defference between static inner and non
    static inner class…….
    I)In the case of declaring data memeber and member method
    , non static inner class cannot have static data member
    and static member method……
    But Static Inner class can have static and
    non static data member and member method……

    II) In the case of creating instance, the instance of non s
    static inner class is created with the reference of
    object of outer class in which it is defined……this
    means it have inclosing instance …….
    But the instance of static inner class
    is created with the reference of Outer class, not with
    the reference of object of outer class…..this means it
    have not inclosing instance…
    For example……
    class A
    {
    class B
    {
    // static int x; not allowed here…..

    }
    static class C
    {
    static int x; // allowed here
    }
    }

    class Test
    {
    public static void main(String… str)
    {
    A o=new A();
    A.B obj1 =o.new B();//need of inclosing instance

    A.C obj2 =new A.C();

    // not need of reference of object of outer class….
    }
    }

  4. Anonymous says:

    some thing

  5. Anonymous says:

    thanks for the example

  6. rajasekhar says:

    Please sir i need more explanation on this

  7. RaMesh says:

    The functionality of static and non static inner classes are like static and nonstatic instance variables in a class.

  8. Kishore says:

    In Inner Classes we have 4 types
    1) Normal Regular Inner classes
    2) Method Local Inner Classes
    3) Ananmuos Inner Classes
    4) Static Nested Inner Classes

    1) Normal Regular Inner classes:
    a) These classes will not have the static members in it
    b) if we want to access this class we have to access through the Outer class only i.e, only Outer class members will access this class
    c) we cant Execute inner class directly from the Command Prompt because it doesnt contain the main method

    eg:
    public class Outer{
    class Inner {
    public void m1(){
    System.out.println(“—–Inner—-“);
    }
    }

    public static void main(String[] a){

    Outer o = new Outer();
    // we can access the inner class just liek the other member variables
    Outer.Inner oi = o.new Inner(); // Outer.Inner oi = new Outer().new Inner();
    oi.m1();

    }

    public void method(){
    System.out.println(“—–normal method—-“);

    }

    }

    Run:
    > java Outer
    > java Outer$Inner (here inner class class file name will be like “OuterClassName$InnerClassName.class”)

    2) Method Local Inner Classes;
    a) Innerclasses declared inside a method body : it can access the outer class members but it cant acesss the local variables declared in the method
    b) Inner Classes declared as a Method Parameter

    3) Ananmuos Innerclasses :
    a) a inner classe with out a name
    eg : Runnable r = new Runnable(){
    public void run(){
    System.out.println(“– Inner Class Run Method”);
    }

    };
    4) Static Nested Inner Class:
    a) This is same as the Normal regular Innerclass, but it has the static members available
    b) static members are available so we can run directly the InnerClass
    c) if we want to create the Inner Class Object no need to depend on the Outer Class Object

    public class Outer{
    static class Inner {

    public void m1(){
    System.out.println(“—–Inner—-“);
    }

    public static void main(String[] a){
    System.out.println(“—–Inner class Main method —-“);
    }

    }

    public static void main(String[] a){

    // we can access the inner class just liek the other member variables
    Outer.Inner oi = new Outer.Inner();
    oi.m1();

    }

    public void method(){
    System.out.println(“—–normal method—-“);

    }

    }

    Run:
    > java Outer
    > java Outer$Inner (here inner class class file name will be like “OuterClassName$InnerClassName.class”)

    D:\Practices\Java\Test>java Outer$Inner
    —–Inner class Main method —-

    D:\Practices\Java\Test>java Outer
    —–Inner—-

    Please correct me if i am wrong. i will give update on the remaining two inner classes dependiing on the comments for this.

  9. Anil says:

    Nice description

  10. Shazz says:

    Sir, You write really well so it is a request that please do not end your blog in one liner definition.

  11. Krishna Chaitanya says:

    Hi Kishore, great work, seen a wide explanation, can you explain rest of two with examples

  12. madhumathi says:

    sir i need to learn from basics of java am know little about java to declare and create object can u help me

  13. vinoth says:

    could u feel what ll exactly happen for the following two different codes.

    void display(List list)
    {
    }

    void display(List list)
    {
    }
    will either of them work in the same way..

  14. venkat says:

    you will get compilation error i..e duplicate method

  15. pradeep kumar Palai says:

    Fantastic explanation ,Please elaborate this with example ,means how can u create the object of the inner class .but it is really nice.

    Thank and Regards
    Pradeep

  16. Djhseen says:

    As i know,,
    static methods can be called in the main class without necessity of making an object of the class that contains that method..

    for example u have
    class X { //here’s the class
    int some_method (int a) //here’s the method
    {some operations}}

    class some{public static void main(String[] args){
    int a; //here’s some integer

    X.some_method(a) ; /* we used method (some_method) from the calss a without the necessity of making an object from that class */
    }}

  17. sathiyaraj says:

    Hi Sir,

    Actually i wrote a program in java language, i used ImageIO static method to write the image but my input value is not a static so no error show even result also not showing.

  18. tamilsevi says:

    how to access a non static method in static inner class….?…

  19. Anonymous says:

    class X { //here’s the class
    Static int some_method (int a) //here’s the method
    {some operations}}

    class some{public static void main(String[] args){
    int a; //here’s some integer

    X.some_method(a) ; /* we used method (some_method) from the calss a without the necessity of making an object from that class */
    }}

    if u put static just before the some_method then u can do like this else u will get an error.

  20. Anonymous says:

    Inside static inner class,create an object of enclosing class(outer class)which has non static method and use this object to call non static method.

    public class learnStatic {

    public void getValue1(){
    int b=12;
    System.out.println(“Inside getValue1 : b :” + b);

    }
    static class Inner{
    static void getValue2(){
    getInner();
    }
    static void getInner(){

    learnStatic obj = new learnStatic();
    System.out.println(“Here we calling non static method inside static inner class”);
    obj.getValue1();
    }
    }

    }

    public class callStatic {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    learnStatic objStatic = new learnStatic();
    objStatic.getValue1();
    learnStatic.Inner.getValue2();

    }

    }

    ———-

    correct me if I am wrong.

  21. dhiraj says:

    why we can not have an outer class as static ?

  22. Anonymous says:

    you are really correct ashish ….

  23. Anonymous says:

    good

  24. Anonymous says:

    Is it posssible to declare a class as ststic?

  25. Deepak says:

    its really helpful for me..thank a lot.

Comments are closed for this "Difference between static and non-static java inner class." tutorial.