I've been learning Java recently and reached the following conclusion: declaring a static nested type is a meaningless redundancy.
I was reading the answers on the below question and while I do understand the answers (and it cleared my inetial confusion - similar to the one the asker had) Can a Static Nested Class be Instantiated Multiple Times? I also concluded that the word static regarding an inner type is meaningless, for example if we didn't declare the inner type as static, we still wouldn't need to instantiate the outer type to be able to create new objects of the inner type. after all its a nested type and not a member/variable of the outer type. I just tested some code (removed static) and my assumption was right.
Is there anything I missed?
here is the code I tested (Note when I removed Static from KeyEvent Type inside IGameInput, every thing worked fine)
package FrameWork;
import java.util.List;
public interface IGameInput {
//type definitions
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;
}
}
///////////////////////
package FrameWork.Imp;
import java.util.ArrayList;
import java.util.List;
/*
*/
public class Pool<T>
{
public interface PoolObjectFactory<T> {
public T createObject();
}
private final ArrayList<T> freeObjects;
private final PoolObjectFactory<T> factory;
private final int maxSize;
public Pool(PoolObjectFactory<T> factory, int maxSize)
{
this.factory = factory;
this.maxSize = maxSize;
this.freeObjects = new ArrayList<T>(maxSize);
}
public T newObject()
{
T object = null;
if (freeObjects.size() == 0)
{
object = factory.createObject();
}
else
{
object = freeObjects.remove(freeObjects.size() - 1);
}
return object;
}
public void free(T object)
{
if (freeObjects.size() < maxSize)
{
freeObjects.add(object);
}
}
}
////////////////////////
package FrameWork.Imp;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.View.OnKeyListener;
import FrameWork.IGameInput.KeyEvent;
import FrameWork.Imp.Pool.PoolObjectFactory;
public class KeyboardHandler implements OnKeyListener {
boolean[] pressedKeys = new boolean[128];
Pool<KeyEvent> keyEventPool;
List<KeyEvent> keyEventsBuffer = new ArrayList<KeyEvent>();
List<KeyEvent> keyEvents = new ArrayList<KeyEvent>();
public KeyboardHandler(View view) {
PoolObjectFactory<KeyEvent> factory;
factory =
new PoolObjectFactory<KeyEvent>() {
@Override
public KeyEvent createObject() {
return new KeyEvent();
}
};
keyEventPool = new Pool<KeyEvent>(factory, 100);
view.setOnKeyListener(this);
view.setFocusableInTouchMode(true);
view.requestFocus();
}
@Override
public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
if (event.getAction() == android.view.KeyEvent.ACTION_MULTIPLE) return false;
synchronized (this) {
KeyEvent keyEvent = keyEventPool.newObject();
keyEvent.keyCode = keyCode;
keyEvent.keyChar = (char) event.getUnicodeChar();
if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) {
keyEvent.type = KeyEvent.KEY_DOWN;
if (keyCode > 0 && keyCode < 127) pressedKeys[keyCode] = true;
}
if (event.getAction() == android.view.KeyEvent.ACTION_UP) {
keyEvent.type = KeyEvent.KEY_UP;
if (keyCode > 0 && keyCode < 127) pressedKeys[keyCode] = false;
}
keyEventsBuffer.add(keyEvent);
}
return false;
}
}