Basically, I am creating a minigame. I have added a feature where you can add and remove players from the game, but I want to know how I can use the list. So for example, here is the list: Code: public static List<Player> playersInGame = new ArrayList<Player>(); I want to know how to take the players in that list, and (for example) send them all a message. Also, would using their names in the list be more effective and less buggy?
@JDJ It's not recommended to use Player in a List. Just use their names, it will be fine. Just make sure to remove them if they log out etc. You would just iterate through it and send the player in the current iteration a message. But why is the List static?
@JDJ Class one Code: public class Student() { private String name; private int grade; public Student(String name, int grade) { this.name = name; this.grade = grade; } public String getName() [ return this.name; } public int getGrade() [ return this.grade; } } Class two Code: public class Main() { public static void main(String[] args) { Student student = new Student("Joe", 12); System.out.println(student.getName()); } }
@JDJ guys above are right, extreme static leads to ram leakage when not handled correctly, especially when it contains a player. You can keep lists in main class since it is te one that does not get initialized more than 1ce and then pass the list through a constructor, @Skionz provided a great example.