views:

200

answers:

5

G'day all, I have a Player class, which inherits from an ArmedHumanoids class, which inherits in turn from a Humanoids class.

Where and when should I create the Player object so that it is accessible in all my other classes - for example, a selectPlayerRace class?

I know that by extending the Player class it becomes accessible, but I am thwarted because all my other classes extend JFrame. There can be only one extension, not two.

At the moment the NetBeansIDE complains that it cannot locate the Player object when I code for it in selectPlayerRace, after creating the Player object in my Main class.

Obviously, it is happier when I create the Player object in the selectPlayerRace class, but then I can't access the Player object in all my other classes as it is internal to selectPlayerRace.

The Player object is intended to be a universal object accessible in all my other classes. I'm sorry for this dreadfully newbieish question, but I can't figure it out.

A: 

It sounds like you are looking for a static declaration here. As I understand it you want to get a global access to some instance of Player but would not need to extend some other class or implement some interface to do so (which means you don't have to inherit any behaviour, you just want access to one object). You could store the Player instance you want to access static e.g. in a PlayerManagement class. You then put a static method in it like getPlayer(String playerName) which synchronizes access to its array/Vector/whatever containing all Player objects and then returns the object in question (or null if not found). You can link your humanoid-classes into PlayerManagement and vice versa.

Maybe you also want a Player interface. You can implement as many interfaces as you like but only extend one other class but you have to reimplement all functionality since you cannot write any code into an interface, just define what attributes/methods a class implementing that interface must provide.

Energiequant
Oooookay?! Any comment why this has been down-voted? I don't get it?
Energiequant
+2  A: 

You can create a object of your Player class and pass it to the other objects via e.g. constructor or method which accepts Player type.

public class Entry
{
   public static void main(string[] args)
   {
      // initialize a player object
      Player player=new Player("elwynn");
      // initialize some other object which requires player object.
      // since player object needs to be accessed within foo.
      Foo foo = new Foo(player);
      // you are about to use player object within foo.
      foo.MakePlayerPlay(); 
   } 
} 

public class Foo
{

  Player player;
  public Foo(Player p)
  {
   this.player = p;
  }

  public void MakePlayerPlay()
  {
    // you are using player object here
    // which is the same instance you created 
    // within main() in Entry class.
    if(this.player!=null) this.player.play();
  }

}
codemeit
A: 

It is bad style to have global objects, because this

  • will make testing more difficult, and

  • will cause problems when there arises a need to have more than one instances.

It is better to give references to other objects on construction (this is called dependency injection) on a need-to-know basis, or where appropriate to pass references by parameter.

That said, look up the Singleton design pattern.

starblue
Look up the singleton antipattern, and make a note never to use it.
Tom Hawtin - tackline
+3  A: 

The Player object is intended to be a universal object accessible in all my other classes.

Why not just pass this Player object in the constructor of the other classes, or in the methods that you call?

Zach Scrivena
A: 

This sounds like a singleton pattern.

public class Player extends ArmedHumanoid {

  private static Player instance = new Player();

  private Player() {}

  public static Player getIntstance() {
    return instance
  }

}

Then any class can call Player.getInstance() to get access to the Player object.

Clint
That is exactly the solution I needed. It works perfectly in my code. For example, I can call Player.getInstance.classType = "Mage" in the selectClassType class. Thank you so much for solving this problem for me.
elwynn