6

You cannot declare an interface inside a block like below

public void greetInEnglish() {

        interface HelloThere {
           public void greet();
        }

        class EnglishHelloThere implements HelloThere {
            public void greet() {
                System.out.println("Hello " + name);
            }
        }

        HelloThere myGreeting = new EnglishHelloThere();
        myGreeting.greet();
}

In This Oracle tutorial I got "You cannot declare member interfaces in a local class." because "interfaces are inherently static."

I am eagar to understand this with more rational information, why and how interface are inherently static?

and why above code does not make sense?

Thanks in advance to elloborate!

Kevan
  • 1,085
  • 1
  • 9
  • 15
  • I think your question is duplicated. Please have a look at this. http://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface – Du-Lacoste Feb 05 '15 at 11:34
  • @DulithDeCozta No it's not a duplicate at all , [Why can't I define a static method in a Java interface?](http://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface) this is a totally different question – Neeraj Jain Feb 05 '15 at 12:16

3 Answers3

2

I am eagar to understand this with more rational information, why and how interface are inherently static?

because interfaces are implicitly static, and you can't have non-final statics in an inner class.

Why are they implicitly static?

because that's the way they designed it.

and why above code does not make sense?

because of the above reason ,

Now lets make it simple :

What static means - "not related to a particular instance". So, suppose, a static field of class Foo is a field that does not belong to any Foo instance, but rather belongs to the Foo class itself.

Now think about what an interface is - it's a contract, a list of methods that classes which implement it promise to provide. Another way of thinking about this is that an interface is a set of methods that is "not related to a particular class" - any class can implement it, as long as it provides those methods.

So, if an interface is not related to any particular class, clearly one could not be related to an instance of a class - right?

I also suggest you to study Why static can't be local in Java?

Community
  • 1
  • 1
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • I appreciate your efforts but still I have question for this sentence:"if an interface is not related to any particular class, clearly one could not be related to an instance of a class" then why we can have interface within a class? here is interface withing a method that's why not allowed. – Kevan Feb 05 '15 at 13:24
  • I have given that reference for a purpose , So let me explain what was that purpose , if you go through it you will able to understand why static can't be local , as it is a class property . Everything declared inside the class is not instance property , we have static variables which belongs to class . As interfaces are implicitly static , So putting them inside class doesn't make them related to an **instance** of a class . I hope you got my point . – Neeraj Jain Feb 05 '15 at 14:43
  • 'and you can't have non-final statics in an inner class' is not accurate, **static final method** is still not allowed before JDK8, however, we can define **static final variables** in local class. – zhouji Mar 12 '17 at 12:55
  • there are not logical reasons to why it is so, it is just the way it is implemented, mainly due to "lazy" "designers" or lack of rational thought. – mjs Aug 23 '20 at 17:20
0

Any implementations can change value of fields if they are not defined as final. Then they would become a part of the implementation.An interface is a pure specification without any implementation.

If they are static, then they belong to the interface, and not the object, nor the run-time type of the object.

An interface provide a way for the client to interact with the object. If variables were not public, the clients would not have access to them.

Anptk
  • 1,125
  • 2
  • 17
  • 28
0

Your code does not make sense because you define the interface within the body of a method. You can define an interface either at top level or in another class or interface.

You cannot declare an interface inside a block

reference

nrainer
  • 2,542
  • 2
  • 23
  • 35
  • If you have read the question carefully , He has given the code from a [reference](http://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html) and he is asking why we cannot do this . – Neeraj Jain Feb 05 '15 at 12:15
  • Yes, and this is the relevant part of the last paragraph before the code example. – nrainer Feb 05 '15 at 12:38