Search code examples
javalocal-class

Does local class of interface exist?


We known that local class of a class exists (for example: local class inside a method of a class), but can we declare a local class of an interface. I reformat the original of local class as follows:

"Local class (of interface) is a non-static member class which have own name (local name) and it can be used inside a default/static/private method of interface".

So, its definition is that correct or not? because when I tried to declare, no Compile-Time error occurs.

Please help me, thank you very much

I give an example as follows:

public interface GlobalClass {
static void functionB() {
    class Local {}
    
};
default void functionC() {
    class Local {};
};}

Is that correct or not?


Solution

  • Yes, you can create local classes inside any method body, also in static or default methods of interfaces. Classes you create inside methods of interfaces don't really differ from local classes you'd create inside a class method.

    Your definition is correct, except that there's not really much distinction between a local class declared in an interface and a local class declared in a class.

    Some more info on local classes is available in Oracle tutorials: https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html