nhooyr
nhooyr

Reputation: 1204

Why are interfaces static?

Why can I not have a interface inside of a inner class? Why are they inherently static? Sorry if it's a stupid question, I've tried my best to google this again and again but I can't seem to wrap it around my head. As in why cannot I declare these in inner classes/local classes?

Also just as a confirmation, the reason we can have static final variables in a interface is because they do not specify the state or any of that sort of the implementation right? If we lose static and use just a final, we need a instance which makes no sense cause you can't instantiate a interface. Sorry, I really am confused, and I know I should just make another question but I think these two questions are somewhat related.

Upvotes: 8

Views: 1951

Answers (6)

comocoder
comocoder

Reputation: 84

Long story shot:

It's an interesting and sometimes subtle aspect of Java! Even if you don’t explicitly use the static keyword, any interface declared inside a class is implicitly static by definition. --ChatGPT

This means you can reference it without creating an instance of the enclosing class, and it behaves independently of any specific instance of the outer class.

I might be late to the party here.
But as I'm learning more about Java, I find it really interesting. chatGPT told me this :

Double-check your understanding. Java won't allow truly non-static interfaces nested in classes.

which has brought me here so to say.
I was debating this with ChatGPT, as it stated that inner interfaces are static.
I removed the 'static' keyword from its definition and my code worked, only for it to later tell me that even without the keyword, an inner interface is still static.

Upvotes: 0

Shubhangi Jain
Shubhangi Jain

Reputation: 1

After Java 16 release we can have static members inside Inner classes and static variables can be declared if they are final or effectively final. See this image https://docs.oracle.com/en/java/javase/17/language/java-language-changes.html#GUID-8FD2B5E3-46C7-4C6C-8E8A-64AB49ABF855

Upvotes: 0

user207421
user207421

Reputation: 310911

Why can I not have a interface inside of a inner class?

Because interfaces are implicitly static: JLS §8.5.1:

A member interface is implicitly static (§9.1.1). It is permitted for the declaration of a member interface to redundantly specify the static modifier.

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.

why cannot I declare these in inner classes/local classes?

Because they're implicitly static.

the reason we can have static final variables in a interface is because they do not specify the state or any of that sort of the implementation right?

Right.

If we lose static and use just a final, we need a instance

Right.

which makes no sense cause you can't instantiate a interface.

Yes you can. You can instantiate a class which implements the interface, or you can instantiate a method-local anonymous implementation of it. The real issue here is multiple inheritance of interfaces.

Upvotes: 4

drew moore
drew moore

Reputation: 32680

Think about what static means - "not related to a particular instance". So, as you point out, 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?

*Note, as @Owlstead points out, there are ways of defining interfaces within classes. But, for the purposes of wrapping your head around what an interface is (which seems to be what you're working on), I would ignore those possibilities for now as they distract from and possibly obscure the purpose of interfaces in general.

Upvotes: 9

ruakh
ruakh

Reputation: 183311

Why are they [interfaces] inherently static?

The difference between a static and a non-static nested class is in whether their instances have implicit references to enclosing instances (of the containing class), as well as to local variables from the containing scope. Before Java 8, there was no way for an interface to make use of such implicit references, because an interface could not initialize any non-static fields or provide any method implementations. (It still can't initialize non-static fields, though now it can provide default method implementations.) So before Java 8, there was no meaning in a non-static nested interface.

Also, from an implementation standpoint, these implicit references are implemented as an extra fields on the inner class, and they also require extra arguments to the inner-class constructor (in order to initialize these fields). Interfaces don't have fields, or constructors, so there's no way to implement this.

(Note: I don't usually recommend trying to understand language design decisions in terms of the implementation, because a single language feature can have many different correct implementations. But I think this is one case where understanding the implementation helps to understand the specification, hence the previous paragraph.)

Upvotes: 5

Eagle
Eagle

Reputation: 163

You cannot have an interface inside of an inner class because an inner class only exists within the context of an instance of an 'outer class'. Since this is the case, your interface would be de facto non-static.

You can, however have an interface inside of a nested class. See @owlstead answer. By placing the 'static' keyword on a the declaration of an 'inner class', it becomes a first class citizen, referencable from outside the outer class and (mostly) independent of the context of the outer class. Nested classes can be instantiated outside of the outer class; inner classes cannot.

Upvotes: 1

Related Questions