0

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?

locobe
  • 69
  • 7
  • 1
    I am not getting it. You are asking whether a local class can be declared in interface methods (static and/or default)? But then you give the answer by providing code that compiles fine? – Seelenvirtuose Jul 18 '20 at 09:27
  • That is correct, It means, any class defined inside the private/default/static method, That is available to only scope of that method, It is not accessible for any other method. It is just like local variables. – Pandey Amit Jul 18 '20 at 09:30
  • Does this answer your question? [Inner class within Interface](https://stackoverflow.com/questions/2400828/inner-class-within-interface) – Ayush Jul 18 '20 at 13:21
  • @Ayush an inner class is something else than a local class. – Mark Rotteveel Jul 18 '20 at 14:41

1 Answers1

1

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

Forketyfork
  • 7,416
  • 1
  • 26
  • 33