4

I have been studying about static inner class in java. But i am not clear whats the point of using static inner class or inner class.

class A{

    static class B{

    }

    public static void main(String[] args) {

        B b=new B();

    }

}

or

class B{}
class A{
    public static void main(String[] args) {

        B b=new B();

    }

}
1
  • Welcome to Stack Overflow! To keep the site as useful as possible, we try to avoid having the same question posted multiple times. The next time you have a question, we would appreciate it if you would search Stack Overflow to make sure someone else hasn't already asked the same thing =) Also, you should find the FAQ helpful if you haven't read it yet: stackoverflow.com/faq
    – Kevin
    May 13, 2013 at 14:33

5 Answers 5

11

Non-static inner classes have an automatic reference to their enclosing class. A static inner classes only relationship to its enclosing class is that you have to reference it via the enclosing class' name: EnclosingClass.StaticInnerClass.

Non-static inner classes are good when you want to reference some of the data from the parent class.

A static inner class is good when you just want to associate the inner class with the enclosing class without dragging it along for the ride.

In other words, a non-static inner class can prevent the enclosing class from being garbage collected, since it has that reference, while a static inner class will never do that.

2

There is technical difference:

class A {
    private static int x = 42;  //the answer
    public static class B {
        int showX() {
            return x; // only static class can it
        }
    } 

}

But it isn't the main point. If class B is used only by class A it's good to make it inner because some classes in one package may want to have utility class with same name.

1

When your inner class is considered as part of your "object", use inner class. Indeed, you would be able to access private,package,protected and public fields from your wrapping class.

"Drawback" is: An inner class can't exist without it's wrapping class instantiated, that is logically due to the first sentence.

Otherwise, if you consider:

  • The behaviour of your nested(called also static inner) class isn't considered as reusable by external class since maybe too specific.
  • some related fields of one class as being so much related that you want to make a class wrapping them. This will get your code more understandable and cleaner.

then choose to make a static class.

Moreover, since static (meaning outside the life cycle of any object), a nested class may instantiate without regarding its wrapping class/object.

0

The result is the same, but if B is a little Class that A uses it just makes more sense to put it into A.

0

By making a nested classes can be static, you can use the nested class without having an instance of the outer class.

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. ...

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

See tutorial here

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.