Reputation: 279
I've started preparing myself for the OCJP7 exam and I found this chapter that seems to be very complicated.
Let's say I have this code:
class Outer1{
interface InnerInterface{
String x = "test";
}
class InnerClass{
String x = "test";
}
}
class Outer2{
static interface NestedInterface{
String x = "test";
}
static class NestedClass{
String x = "test";
}
}
class Main{
public static void main(String [] args){
String s1 = Outer1.InnerInterface.x;
String s2 = new Outer1().new InnerClass().x;
String s3 = Outer2.NestedInterface.x;
String s4 = new Outer2.NestedClass().x;
}
}
Could you tell me why we can access Outer1.InnerInterface.x
and Outer2.NestedInterface.x
in the same manner? Inner interfaces are static by default? I'm trying to make some connections to make them more clearly.
Upvotes: 11
Views: 1022
Reputation: 19837
Could you tell me why we can access
Outer1.InnerInterface.x
andOuter2.NestedInterface.x
in the same manner?
Because they're both static
fields.
From Java Language Specification, Chapter 9. Interfaces, point 9.3 Field (Constant) Declarations:
Every field declaration in the body of an interface is implicitly
public
,static
, andfinal
.
Inner interfaces are static by default?
Yes.
From 8.1.3 Inner Classes and Enclosing Instances:
Member interfaces (§8.5) are implicitly
static
so they are never considered to be inner classes.
Upvotes: 3
Reputation: 41
You are right. The keyword static
for nested interfaces is unnecessary and redundant. A nested interface is always static.
From a logical point of view it also makes no sense that a interface definition is bound to a specific instance of the outer class.
Upvotes: 1
Reputation: 3200
From Oracle's Java Tutorial :
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)
Interfaces cannot be instantiated. Thus they only make sense as static. Declaring a nested interface as static is redundant.
Also, the exercise uses confusing names for the interfaces.
Both InnerClass
and NestedClass
are nested classes. But only InnerClass
is an inner class since "inner class" means "non-static nested class".
Similarly one might expect InnerInterface
to be an "inner interface" meaning "non-static nested interface"; but such a thing does not exist. Both InnerInterface
and NestedInterface
are nested interfaces and none of them are inner interfaces.
Upvotes: 11
Reputation: 178253
Yes, nested interfaces are implicitly static
. Section 8.5.1 of the JLS states:
A member interface is implicitly
static
(§9.1.1). It is permitted for the declaration of a member interface to redundantly specify thestatic
modifier.
Because of this, you can access x
through the nested interface in the same manner through outer classes Outer1
and Outer2
-- both nested interfaces, InnerInterface
and NestedInterface
are static
.
Upvotes: 7