1

I am trying to add newly created objects to an ArrayList in the constructor of a class. The new objects are being created in another class in the main method.

Main method:

public static void main(String[] args) {
    // TODO code application logic here
    Player p1 = new Player("Peter");
}

My Player class:

public class Player {

protected static int age;
protected static String name;
protected static ArrayList players = new ArrayList();

Player(String aName) {
    
    name = aName;
    age = 15;
    players.add(new Player()); // i know this doesn't work but trying along these lines
    
   }
}
5
  • 5
    name and age should not be static. Commented Oct 18, 2013 at 18:46
  • 1
    You could try players.add(this) which refers to your current instance, but I my suggestion would be to revise your structure because I think it should not be the Player's duty to remember which players have been created so far. This functionality should be delegated to a dedicated entity Commented Oct 18, 2013 at 18:46
  • There could be legitamate reasons to keep a list of all players inside Player. I'm not saying I would do it this way, but you can make a perfectly workable program like this Commented Oct 18, 2013 at 18:47
  • I agree, there is always the possibility. I can't know what his exact thoughts are, but following his example and the usage of the static variables, I reached the above conclusion Commented Oct 18, 2013 at 19:00
  • It almost looks like a implementation of a linked list that uses ArrayList instead of Player previous;Player next I would bet that there will be a method next & previous. Otherwise I agree with @Grove that it should be delegated elsewhere Commented Oct 18, 2013 at 19:02

3 Answers 3

6

You have to edit the line

players.add(new Player());

to

players.add(this);

Also, there is no need to make the age and name static

I suggest you should have the following code instead

import java.util.ArrayList;

public class Player {

protected int age;    //static is removed
protected String name;  // static is removed
protected static ArrayList<Player> players = new ArrayList<Player>();  //this is not a best practice to have a list of player inside player.

Player(String aName) {

    name = aName;
    age = 15;
    players.add(this); // i know this doesn't work but trying along these lines

   }


public static void main(String[] args) {
    // TODO code application logic here
    Player p1 = new Player("Peter");
}


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

2 Comments

Just adding a default constructor would lead to creation of a player having a different new player added to the arraylist, the OP is using new Player() to explain the concept of "this"
I managed to do it in the end and it was exactly along these lines. Thanks.
2

It sounds like you're actually asking how to refer to the instance you're constructing.
That's what the this keyword does.

4 Comments

no. he is failing cause new Player() wont work, due to no default constructor.
@DarthVader: Yes, but that doesn't make sense anyway.
@DarthVader I think the OP is using new Player() to express the concept of "this"
I think there is a design and understanding the concepts problem there. such as static props, and Player has players didnt make much sense to me
0

This post is old, but for someone with similiar needs I suggest doing it like this:

Main class:

public static void main(String[] args) {
    //TODO code application logic here
    java.util.List<Player> players = new java.util.ArrayList<>(); //list to hold all players
    Player p1 = new Player("Peter"); //test player
    players.add(p1); //adding test player to the list
    players.add(new Player("Peter")); //also possible in this case to add test player without creating variable for it.
}

Player class:

public class Player {

    private final int age = 15; //if you always going to use 15 as a age like in your case it could be final. if not then pass it to constructor and initialize the same way as the name.
    private String name;

    Player(String name) { //variable name can be the same as one in class, just add this prefix to indicated you are talking about the one which is global class variable.
        this.name = name;
    }

}

Generally you should avoid using static keyword on variables which supposed to be different in each instance of that class. Avoid having object of itself inside.

Comments

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.