Is it possible to create an array of static classes in Java? For example:
SceneObject[] scenes = {Loading.class, Menu.class};
// Loading and Menu extend SceneObject
We need to call static methods via the array, not instantiate them.
EDIT:
The following is what we are trying to accomplish. We could alternatively use many switches, but it sounds redundant to add every object to every switch in every method.
package net.bitworm.gameengine;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import net.bitworm.scenes.*;
public class SceneController {
public enum Scene{
LOADING_SCENE,
MENU,
SCENE_1
}
public static SceneObject[] scenes = {new Loading(), new Menu()};
public volatile static Scene currentScene = Scene.LOADING_SCENE;
public static void setScene(Scene newScene){
currentScene = newScene;
System.out.println("Switched to " + currentScene.toString());
}
public static void update(GameContainer container, int delta){
scenes[currentScene.ordinal()].update(container, delta);
}
public static void render(GameContainer container, Graphics g){
scenes[currentScene.ordinal()].render(container, g);
}
public static void mouseMoved(int oldx, int oldy, int newx, int newy){
scenes[currentScene.ordinal()].mouseMoved(oldx, oldy, newx, newy);
}
public static void mousePressed(int button, int x, int y){
scenes[currentScene.ordinal()].mousePressed(button, x, y);;
}
public static void mouseReleased(int button, int x, int y){
scenes[currentScene.ordinal()].mouseReleased(button, x, y);
}
public static void mouseWheelMoved(int change){
scenes[currentScene.ordinal()].mouseWheelMoved(change);
}
public static void keyPressed(int key, char c){
scenes[currentScene.ordinal()].keyPressed(key, c);
}
public static void keyReleased(int key, char c){
scenes[currentScene.ordinal()].keyReleased(key, c);
}
You need to differentiate between classes and objects. For example, you might have:
SceneObject[] scenes = { new Loading(), new Menu() };
or
Class[] classes = { Loading.class, Menu.class };
It's not clear from your question which you mean, but hopefully that should satisfy either case... note that you can't have generic arrays, so with the Class[]
you can't specify that each class must extend SceneObject
.
EDIT: Now we've got a bit more information, it sounds like you've got this:
abstract class SceneObject {}
class Menu extends SceneObject {
static void foo() {
}
static void bar() {
}
}
class Loading extends SceneObject {
static void foo() {
}
static void bar() {
}
}
The two foo
methods here are completely unrelated - you can't use polymorphism to call them, because they're static methods. If you want to use polymorphism - i.e. call a method knowing which signature you want to call, but with an implementation that depends on the target of the call - you need instance methods:
abstract class SceneObject {
abstract void foo() {
}
abstract void foo() {
}
}
class Menu extends SceneObject {
@Override void foo() {
}
@Override void bar() {
}
}
class Loading extends SceneObject {
@Override void foo() {
}
@Override void bar() {
}
}
Then you can write:
SceneObject[] scenes = { new Loading(), new Menu() };
...
for (SceneObject scene : scenes) {
scene.foo();
scene.bar();
}
See more on this question at Stackoverflow