0

I'm very much new to the concept of OOP and for a project I'd have to make a card game with 2 players. I created a class called Player which has an private attribute called playerNumber, and many others. I created a method called playTurn which needs to use Player's private attributes such as playerNumber.

I tried to use it like this:

public static void playTurn(){  
    System.out.println("It's Player " + this.getPlayerNumber() + "'s turn.");
}

but Eclipse would give ask me either to make the getter method getPlayerNumber() static or the private attribute playerNumber static.

Is it possible to have remain the private PlayerNumber without static and still use the that attribute in another method, but in the same class?

beyonchayyy
  • 93
  • 2
  • 12
  • you should have one class for player informations such as its number, and another class for your playTurn method. You create a new player twice to get two players, and get their attributes with your getter in the Player class like currentPlayer.getPlayerNumber – skoll Jun 29 '14 at 11:47
  • 1
    Your problem is that `this` makes no sense in a `static` method, because `this` means the object on which the current method is being run, and `static` means that the method is not run on any particular object. Most likely, you want `playTurn` not to be `static`. – Dawood ibn Kareem Jun 29 '14 at 11:49

1 Answers1

3

You cannot reference a non-static method or variable from a static context.

It looks like playTurn() should not be static since it requires access to an instance of Player.

See this answer

Community
  • 1
  • 1
rob
  • 1,286
  • 11
  • 12