A
A
Anatoly2017-07-25 14:08:52
Java
Anatoly, 2017-07-25 14:08:52

How to programmatically name an instance of a class?

I'm learning Java, I ran into such a problem, you need to enter the player's name, and based on this name a new object of the Player class is created, but I realized that I can't and don't know how to do it. And how without it to create other objects, the number of which will be different?

public void gameStart() {
String name;
Scanner input = new Scanner(System.in);
System.out.println("Input your player name:");
name = input.next();
System.out.println("You name now: " + name);
... here I wanted to create an object, but I realized that I can't
}

In general, the problem is that wherever an object is needed, I need to write a construction like:
Team ivan = new Team("Ivan");

How to do it dynamically? In general, I want an instance of the class to be created each time the method is executed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2017-07-25
@TonyWrong

Use, for example List :
and add your players there:
list.add(new Player(name) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question