Reputation: 1
I learnt that, to access the static member class, the syntax is OuterClass.NestedStaticClass
.
For the given below interface Input
,
interface Input{
static class KeyEvent{
public static final int KEY_DOWN = 0;
public static final int KEY_UP = 0;
public int type;
public int keyCode;
public int keyChar;
}
static class TouchEvent{
public static final int TOUCH_DOWN = 0;
public static final int TOUCH_UP =0;
public static final int TOUCH_DRAGGED = 2;
public int type;
public int x, y;
public int pointer;
}
public boolean isKeyPressed(int keyCode);
.....
public List<KeyEvent> getKeyEvents();
public List<TouchEvent> getTouchEvents();
}
below is the implementation Keyboard
,
class Keyboard implements Input{
....
@Override
public List<TouchEvent> getTouchEvents() {
TouchEvent obj1 = new TouchEvent();
TouchEvent obj2 = new TouchEvent();
List<TouchEvent> list = new ArrayList<TouchEvent>();
list.add(obj1);
list.add(obj2);
return list;
}
}
In this implementation Keyboard
, I did not require to use Input.TouchEvent
for below lines of code.
TouchEvent obj1 = new TouchEvent();
TouchEvent obj2 = new TouchEvent();
List<TouchEvent> list = new ArrayList<TouchEvent>();
But for the below implementation, I had to use Input.TouchEvent
,
public class NestedClassInInterface {
public static void main(String[] args) {
Input.TouchEvent t = new Input.TouchEvent();
}
}
How do I understand this?
Upvotes: 1
Views: 122
Reputation: 279910
From the Java Language Specification, concerning members of an interface type
The body of an interface may declare members of the interface, that is, fields (§9.3), methods (§9.4), classes (§9.5), and interfaces (§9.5).
and concerning the scope of a declaration
The scope of a declaration of a member
m
declared in or inherited by a class typeC
(§8.1.6) is the entire body ofC
, including any nested type declarations.
The members of a class type are all of the following:
- [..]
- Members inherited from any direct superinterfaces (§8.1.5)
So, the type TouchEvent
is declared in the type Input
. It is a member of Input
. Keyboard
implements Input
and therefore inherits its members. TouchEvent
is therefore in scope in the body of Keyboard
. You therefore don't need to qualify its use with its enclosing type.
Upvotes: 4
Reputation: 24157
Quoted from doc:
The scope (§6.3) of a member (§8.2) is the entire body of the declaration of the class to which the member belongs. Field, method, member class, member interface, and constructor declarations may include the access modifiers (§6.6) public, protected, or private. The members of a class include both declared and inherited members (§8.2)
So it means class Keyboard
also has members of Input
in its scope and can access them directly.
As your class Keyboard
implements interface Input
it means Keyboard
is Input
. And it will be able to access the static nested class directly without using reference of outer interface. All interface fields are also static by default but an implementation class can access them directly.
But when you try to access it in NestedClassInInterface
(which is not Input
) it needs reference of outer class/interface which is Input
in this case. You can try removing implements Input
from declaration of Keyboard
and will see what I mean.
Upvotes: 0