-1

Currently, I have a couple of static objects that I need to access from multiple other classes.

public static OrthographicCamera camera;
public static World world;
public static Player player;
public static TouchpadHandler touchpad;

It just doesn't feel clean or correct to repeatably access those from the other class, like this:

MainClass.world.blabla();

I've tried PASSING a reference to the desired object in a new instance's constructor. Something like this: (just an example)

    ... MainClass.java
OtherClassINeed obj = new OtherClassINeed(world);

    ... OtherClassINeed.java
private World world;
public OtherClassINeed(World world){
    this.world = world;
}

But this seems like it would be incredibly annoying/inefficient once you start needing multiple things (like the 4 shown above).

So I'm wondering: what's the best way to handle such a thing? Thank you!

John
  • 816
  • 5
  • 17
  • 3
    @MrSimpleMind How is that relevant? – Josh M Jan 12 '14 at 04:34
  • http://stackoverflow.com/questions/1568091/why-use-getters-and-setters - its also very unlikely you should be using static anything. – Brian Roach Jan 12 '14 at 04:35
  • @BrianRoach While that is true I should probably be using a getter, it doesn't solve that fact that I would still have to make the 'get'-method itself static in order to access it (and thus the variable also). - That's my question, how can I avoid using the static but still access it from other classes? – John Jan 12 '14 at 04:38
  • Erm ... I don't think you quite understand basic OOP programming; you may want to start with a beginner's book on Java or OOP. Very rarely are you going to use `static` fields, or methods. – Brian Roach Jan 12 '14 at 04:39
  • @BrianRoach I very well do understand OOP, I have the feeling you are misunderstanding my question. – John Jan 12 '14 at 04:41
  • To be frank, you wouldn't be asking this question if you had the most basic grasp of OOP programming, or Java. Please understand there is no animosity here, merely an observation based on what you have posted. – Brian Roach Jan 12 '14 at 07:49

2 Answers2

0

I don't see using static members of MainClass a bad thing, you can do following if you don't like it:

class MainClass {
    public static final OrthographicCamera camera;
    public static final World world;
    public static final Player player;
    public static final TouchpadHandler touchpad;

    static {
        // initialize static members
    }
}

class OtherClass {
    public static final OrthographicCamera camera = MainClass.camera;

    // now access camera locally.   
}

By the way I believe your main code smell is using statics, if you need singletons, convert them to object members (non final) and have a singleton instance of MainClass maybe using an IoC container.

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69
  • Thank you. Do you know, efficiency-wise, which method is more optimal? – John Jan 12 '14 at 04:45
  • What do you mean by optimal? Performance? If you are not developing a real-time control application, don't think of micro-benchmarking and tuning; go for better design which is using non-static members. – Amir Pashazadeh Jan 12 '14 at 04:47
  • Well, I am creating a game so I believe performance is rather important, unless it is extremely minimal in this situation. – John Jan 12 '14 at 04:53
  • It's worth it to also mention you can `import static com.acme.MainClass.camera;`. This will basically have the same impact: you can reference `camera` instead of `MainClass.camera`. @user3186618 Global state or non-global state isn't going to be something with an effect on performance. Rather, it is a paradigm issue. Having too much global state is an indication of possibly poor design. – Radiodef Jan 12 '14 at 05:33
  • If it is a game, forget about performance, believe me using a non-static members, and having a singleton `MainClass` using an IoC would not affect your performance, but improves your design. – Amir Pashazadeh Jan 12 '14 at 14:00
-1

You should make the objects private static in the Main class and then create static getters there, and then call them to retrieve final references to them in your other classes and therefore no need to pass them into the constructor:

MainClass.java

private static World world;
private static Player player;

...

public static World getWorld() {
    return world;
}

public static Player getPlayer() {
    return player;
}

OtherClassINeed.java

private final World world = MainClass.getWorld();
private final Player player = MainClass.getPlayer();
anddev84
  • 1,473
  • 12
  • 30
  • Why the downvote? The use of static variables aside, based on the OP's criteria this is a valid answer. – anddev84 Jan 12 '14 at 04:42