I would like these objects to print the random names in the array only once across all the objects declared in main. My problem is that it would show the names twice or more.
public class Fighters {
private static String[] names = { "Terminator", "Slicer","Ninja", "cow", "Robot", "littlegirl" };
private static int id = 0;
public Fighters(){
id++;
name = names[(int) (Math.random() * names.length)];
}
public String toString(){
return String.format(id+" Name:%-5s, name);
}
}
In main()
I have these objects
Fighters a1 = new Fighters();
System.out.println(a1.toString());
Fighters a2 = new Fighters();
System.out.println(a2.toString());
Fighters a3 = new Fighters();
System.out.println(a3.toString());