2

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..

Dilini Peiris
  • 446
  • 1
  • 6
  • 16
  • "But i want to know if there's a way to access the local class LocalStatic from an outside class" No, because it's scoped to the body of method `m`. – Andy Turner Oct 15 '17 at 13:43
  • Local Classes are only accessible within the method you've defined them. – QBrute Oct 15 '17 at 13:44

4 Answers4

6

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • interesting.. thanks =) we can do this by extending the local class LocalStatic from another outside class as well right? – Dilini Peiris Oct 15 '17 at 14:01
1

"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

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.

Oleg
  • 6,124
  • 2
  • 23
  • 40
0

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.

cpp beginner
  • 512
  • 6
  • 23
  • 2
    Like me. I would have failed a Java certification for this. I knew about anonymous classes of course, but this construct was not known by me. – M. le Rutte Oct 15 '17 at 13:47
  • But anonymous inner classes are type of a local class right? We use anonymous inner class when using functional interfaces – Dilini Peiris Oct 15 '17 at 14:19
  • I'm not 100% sure of the terminology but I think a local class refers to a named class defined in a method/constructor/initializer, so anonymous classes don't count. – cpp beginner Oct 15 '17 at 14:21
  • 1
    \cppbeginner The [jls](https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3) agrees with you.@DiliniPeiris You can use anonymous inner class to implement a functional interface but lambdas are not anonymous classes – Oleg Oct 15 '17 at 14:27