Reputation: 2633
I'm reading a book on android game development and I've come across the first patch of code:
public static class KeyEvent {
public static final int KEY_DOWN = 0;
public static final int KEY_UP = 1;
public int type;
public int keyCode;
public char keyChar;
}
It is my understanding that anything static means there can only be one instance of it.
If there can only ever be one instance of KeyEvent
why are type
, keyCode
and keyChar
not declared static as well?
Upvotes: 0
Views: 120
Reputation: 37660
static
does not really mean "there can only be one instance of it".
If you are using the static keyword in any declaration within a class, static
means "what I declare here is not a member of an instance of the enclosing class, but it is a member of the class itself."
That being said, if you're declaring a nested class (a class within a class) as static, then it means that the nested class declaration does not rely on an instance of the enclosing class, and can be instantiated directly.
Upvotes: 0
Reputation: 11486
Only nested classes can be declared as static
; not outer/normal classes. It allows you to use the static inner class without instantiating the outer class.
Upvotes: 4
Reputation: 12122
Your code is an nested class
. static
modifier means here that this class is accessible without creating outer class object.
Assuming that outer class is called OuterClass
you can call:
KeyEvent ke = new OuterClass.KeyEvent();
Upvotes: 2