Why are interfaces static?

Why can't I have an interface inside an inner class? Why are they inherently static ? Sorry if this is a stupid question, I tried my best to do it again and again, but I can’t wrap it around my head. How and why can't I declare them in inner classes / local classes?

As well as confirmation, the reason we can have static final variables in the interface is because they do not indicate the state or any of these implementation rights? If we lose static and use only the final, we need an instance that does not make sense, because you cannot create an instance of the interface. Sorry, I'm really confused, and I know I just have to ask another question, but I think these two questions are somewhat related.

+5
source share
4 answers

Think about what static means are "not associated with a specific instance." So, as you note, the static field of the Foo class is a field that does not belong to any instance of Foo , but belongs to the class Foo .

Now think about what an interface is - this is a contract, a list of methods that implement its implemented classes. Another way to think about this is that the interface is a set of methods that are "not associated with a particular class" - any class can implement it if it provides these methods.

So, if the interface is not associated with any particular class, obviously, you could not associate it with an instance of the class - right?

* Notice, as @Owlstead points out, there are ways to define interfaces within classes. But, in order to wrap around what an interface is (it seems that this is what you are working on), I would ignore these features at the moment, as they distract and possibly hide the purpose of the interfaces as a whole.

+5
source

Why can't I have an interface inside an inner class?

Because interfaces are implicitly static: JLS Β§8.5.1 :

The element interface is implicitly static (Β§9.1.1). It is allowed to declare a static modifier excessively to declare a member interface.

and you cannot have non-final statics in the inner class.

Why are they implicitly static?

Because that's how they developed it.

why can't i declare them in inner classes / local classes?

Because they are implicitly static.

The reason we can have static final variables in the interface is because they do not indicate state or is any of these implementation options correct?

Right

If we lose static and use only the final one, we need an instance

Right

which makes no sense because you cannot create an interface.

Yes, you can. You can create an instance of a class that implements the interface, or you can create an instance of the local anonymous implementation method. The real problem here is multiple interface inheritance.

+4
source

Why are they [interfaces] inherently static ?

The difference between a nested static and non static class is whether their instances have implicit references to the enclosing instances (of the containing class), as well as local variables from the content area. Prior to Java 8, there was no way for an interface to use such implicit references, because the interface could not initialize any non-static fields or provide any method implementations. (It still cannot initialize non-static fields, although now it can provide a default implementation.) Thus, before Java 8, there was no point in a nested non- static interface.

In addition, from an implementation point of view, these implicit references are implemented as additional fields in the inner class, and also require additional arguments for the constructor of the inner class (to initialize these fields). Interfaces have no fields or constructors, so there is no way to implement this.

(Note. I usually do not recommend trying to understand language design decisions from an implementation point of view, because one language function can have many different correct implementations. But I think this is one of the cases when understanding the implementation helps to understand the specification, therefore the previous paragraph.)

+3
source

You cannot have an interface inside an inner class, because an inner class exists only in the context of an instance of an "outer class." Since this is the case, your interface will be de facto non-stationary.

However, you can have an interface inside a nested class. See @owlstead answer. By placing the keyword β€œstatic” in the declaration of β€œinner class,” he becomes a first-class citizen who can be associated with an outer outer class and (mostly) independent of the context of the outer class. Nested classes can be created outside the outer class; inner classes cannot.

0
source

Source: https://habr.com/ru/post/1207548/


All Articles