1

I have a class of 'Character', Character is non-static. I want my player class to extend Character but to also be static.

I basically want all other objects and classes to be able to access player without having to create and pass a player instance.

What's the best why to achieve this?

5
  • 1
    Bad idea to name your class the same as the java.lang.Character class Commented Jun 12, 2011 at 4:48
  • Very true, I am narrowing it down to the package but I think I might change it. Commented Jun 12, 2011 at 4:50
  • 1
    Are you sure you understand what "static" means when dealing with classes? A top-level class cannot be declared static, nor non-static. Commented Jun 12, 2011 at 4:52
  • Correct, what I mean though is, I have non-static methods in Characters that I need to be static in Player. Commented Jun 12, 2011 at 4:55
  • It sounds like your current approach to this is misguided, but without more info about what the actual goal is I'm not sure we can recommend anything better. Commented Jun 12, 2011 at 5:23

2 Answers 2

3

The only nice way I can think of is actually not an extension but a wrapper:

class Player {
    private final static Charachter me = new Character();

    public static doSomething(){ me.doSomething(); }
}

Of course you can also extend AND wrap:

class Player extends Character {

    private final static Player me = new Player();

    // if you don't want anyone creating player objects
    // make the constructor private:
    private Player(){ super(); }

    public void doSomething(){
        // stuff
    }

    public static void staticDoSomething(){ me.doSomething(); }
}

Or, actually, since your goal is just to guarantee that there is a single player object, you can forget about making the methods static, but hide the constructor(s):

class Player extends Character {

    private static Player thePlayer = null;

    public static Player getPlayer(){
        if( thePlayer == null ){
            // Create the player
            thePlayer = new Player();
        }
        // There is a valid player object, so return it.
        return thePlayer;
    }

    // hide the constructor(s) by making them private:

    private Player(){ super(); }
}

That ensures that the only way to get a Player is to call Player.getPlayer(), and that it always gives you the same object (you never create more than one).

Sign up to request clarification or add additional context in comments.

4 Comments

In the biz, we call this 'favoring composition over inheritance' ;)
Hey, I learned a new term. Also added a non-composition solution.
...aand I didn't read Mark Peters' solution but the third option is pretty much a copy of it.
Okay, that new solution looks to be the best. I would have never of thought to do it like that. Thanks!
2

Really it seems like you just want a global variable. This is often accomplished through the Singleton pattern:

public class Player extends Character {
    private static final Player humanPlayer = new Player();

    private Player() {
    }

    public static Player getHuman() {
        return humanPlayer;
    }

    //...
}

//...
Player.getHuman().move(2);

There should be very little need for those methods in Player to be static. You're sacrificing good design for a tiny bit of convenience (that will probably bite you later anyway).

Personally I favour dependency injection over global state about 95% of the time. When a method needs to have access to the player, pass it in. That will let you test your code much more easily and will make your code more conducive to change.

3 Comments

What about classes/objects that need access to player. Such-as my GUI needs to have access to it, so should I really just pass it in?
@Paramount That's the nice thing -- you don't need to pass it in, they can just get the player object with Player.getHuman()
Ah okay, between you can @trutheality I think I understand now. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.