0

How do I put/access data stored to/from a object?

I'm trying to make a very simple game so I made an object array to store information about the the players like so:

     player[] p = new player[4];

then put the player objects into the array like so:

for(int i = 0; i < 4; i++)
{
p[i] = new player();
}

the above should create an array with 4 elements with a player object in each.

now... is where I get lost. is this the syntax for putting data members into a object?

player() = {name, turn, rolledNumber}

// String name, boolean turn, int rolledNumber

//got this off wiki probably not a safe bet, but after searching for hours on the Oracle Tutorial site and other java tutorial sites I came up empty handed. I have seen this done with arrays so maybe I thought it would be similar in some aspects.

sorry if this is a empty headed question, I'm just beginning to learn java as a hobby.

john doe
  • 3
  • 3
  • You should follow correct Java naming conventions (capitalize class names) – tckmn Feb 05 '13 at 01:21
  • Please post the code for the `player` class. Also, note that the Java naming conventions suggest that class names should start with upper case and variable names should start with lower case. – Code-Apprentice Feb 05 '13 at 01:35

2 Answers2

1

Rather than use a default constructor for Player, you could add one that takes the arguments for name, turn & rolledNumber, then you could use:

for(int i = 0; i < 4; i++) {
  p[i] = new Player(name, turn, rolledNumber);
}

The individual class member variables can still be accessed via setters & getters. This is known as encapsulation.

Java code conventions indicate that classes take an initial capital letter, so player has been writen as Player here in accordance with the guidelines.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • I see... Is there a way to retrieve an individual data member from those objects. would it be something like: `player(name);` for just the `boolean turn` or would I have to list all the parameters like in method calling, then use some manual way of extracting the individual data member? – john doe Feb 05 '13 at 01:26
  • You would use a _getter_ like `player.getName()`. Note `player` here is an _instance_. See [Setters & Getters](http://stackoverflow.com/questions/2036970/tutorial-on-getters-and-setters). – Reimeus Feb 05 '13 at 01:43
1

In order to store information into state (i.e. in this case name, turn, rolledNumber) variables within the Player class, you need to either set them in a custom constructor, make them public (generally a bad idea), or provide getter/setter methods. Given that you will likely want to change the data in turn and rolledNumber later, but not name one possible solution would be to implement a class with a constructor and getter/setter methods as follows...

public class Player
{
    private String playerName;
    private boolean playerTurn;
    private int rolledNumber;

    public Player(String name, bool turn, int number)
    {
        playerName = name;
        playerTurn = turn;
        rolledNumber = number;
    }

    public String getName() { return playerName; }
    public boolean getPlayerTurn() { return playerTurn; }
    public void setPlayerTurn(boolean turn) { playerTurn = turn; }

    ...

}

Then you call the constructor as per Reimeus' example above.