Is there something called Local Static Inner Class?

142 Views Asked by At

i was just experimenting with inner classes and came across this idea of having local yet static inner class... well i made an inner class inside a static method.. well it's just simple as that.. Here's the example i did

  class Outer {

    static void m() {
        class LocalStatic {

            void s() {
                System.out.println("static local inner class method");
            }

        }
    }
}

class Demo {

    public static void main(String args[]) {

        Outer.m();
    }
}

This doesn't give any compile error.

I know how to access the static method m. But i want to know if there's a way to access the local class LocalStatic from an outside class.. Well as to my understanding, we can't access something inside a method right? Hence i can't access either LocalStatic or any methods or attributes inside that local class from outside of the class Outer Just wanted to make sure..

4

There are 4 best solutions below

1
Sergey Kalinichenko On BEST ANSWER

I want to know if there's a way to access the local class LocalStatic from an outside class

There isn't a way to do that. Local classes are, well, local, so the only way to access them is from the method in which the class is in scope*.

You can access objects of a local class using non-local base class or an interface:

interface SomeInterface {
    void s();
}
class Outer {
    static SomeInterface m() {
        class LocalStatic implements SomeInterface {
            public void s() {
                System.out.println("static local inner class method");
            }
        }
        return new LocalStatic();
    }
}

Now you can write

SomeInterface i = Outer.m();
i.s();

Demo.

* It goes without saying that there is also a way to access these classes through reflection, but that is outside capabilities of Java language itself.

4
cpp beginner On

You can declare a class inside any method or constructor or initializer. Such a class is called a local class. Whether the method is static or not is not relevant. It is not possible to refer to such a class from any part of the code other than the method or constructor or initializer it is declared in.

Local classes are almost never used. I have known people who have been professional java programmers for years who were not aware that classes could be declared inside methods.

0
PresentProgrammer On

"Hence i can't access either LocalStatic or any methods or attributes inside that local class from outside of the class Outer Just wanted to make sure.." The scope is even smaller than Outer class - you can access LocalStatic class only in m() method.

1
Oleg On

The answer to the question in your title is that you can declare a local inner class in a static context (static method or static initializer) but then it won't have an enclosing class instance. So it makes some sense to call it

Local Static Inner Class

or maybe

Local static nested class

I have never seen anyone call them that or in fact use them.