1

To my knowledge, normally in Java, something that is static means you can't create an instance of it. But if that is the case, why can you create an instance of a static nested class? Doesn't that defeat the purpose of something being static?

appljuice
  • 53
  • 1
  • 5
  • 1
    The `static` part of static nested classes really refers to their behavior within their enclosing parent classes. Obviously, you can create an instance, but the class itself behaves as a top level member of the parent class and can only refer to static fields. – Tim Biegeleisen Jan 11 '19 at 06:47
  • static really just means it doesn't belong to any instance. That's true of methods, variables, and classes. In the latter case, it specifically means that you can create that nested class without creating or referring to an instance of its enclosing class. – yshavit Jan 11 '19 at 06:49
  • 4
    `abstract` means (roughly) "cannot make an instance" ... not `static`. – Stephen C Jan 11 '19 at 06:55
  • For some background see the section *Why this weirdness?* in https://stackoverflow.com/a/24953859/823393 – OldCurmudgeon Jan 11 '19 at 07:03

3 Answers3

4

To my knowledge, normally in Java, something that is static means you can't create an instance of it.

That's not right. static is more "does not belong to any specific instance, but to the type/class itself".

Think of a static class in the context of the enclosing class. A static class is a static member, meaning that the nested class is not tied to any particular instance of the enclosing class.
The implication of this is that you can create an instance of the nested class without having to create an instance of the outer class first.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
0

something that is static means you can't create an instance of it.

That's not true. Not only can you not create instances of static fields, you can't create instances of non-static fields either, because it is a field, not a class.

static just means that you don't need to create an instance of the surrounding class to access it. static members belong to the class itself, instead of instances of that class.

With this definition, static classes makes total sense.

Instances of static classes can be created without creating instances of their outer class:

class Outer {
    static class Inner {}
}

Outer.Inner obj = new Outer.Inner(); // no Outer instances created!

Whereas instances of non-static inner classes cannot:

class Inner {
    class Outer {

    }
}

Inner inner = new Inner(); // I have to create this instance, otherwise it wouldn't compile
Inner.Outer outer = inner.new Outer();
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Hi thank you. Can you give an example in what situations static inner classes would make more sense and in what situations non static would make more sense? – appljuice Jan 11 '19 at 07:11
  • @appljuice I can't think of a situation off the top of my head. Does [this](https://softwareengineering.stackexchange.com/questions/238782/why-prefer-non-static-inner-classes-over-static-ones) help? – Sweeper Jan 11 '19 at 07:26
0

Imagine a situation where you have a method in your nested static class and you want to call that method.

Refer to this snippet below:

class OuterClass 
{  

    // static nested class 
    static class StaticNestedClass 
    { 
        void display() 
        { 
            System.out.println("Static Nested Class Display Method");
        } 
    } 

    static void display()
    {
        System.out.println("Outer Class Display Method");
    }

    public static void main(String[] args) 
    { 
        // accessing a static nested class 
        OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); 

        nestedObject.display(); // This calls the display() method of the static nested class
        OuterClass.display(); // This calls the display() method of outer class

    } 
} 

How will you access the display() of static nested class without creating an object of it. Java allows to create an instance of static nested class so that we can access the methods defined within that class.

Hope it helps!!!

sid_bond
  • 135
  • 3
  • 11