-4

Let's say I have this class below:

    public class Player{
}

I could make a new instance of it like this:

Player someone = new Player();

Now I have a .txt file with each line as a potential player:

george
joel
kate
...

I can read these lines and assign them to a String like this:

String name = "george";

How could I make a new Player with the name of "george"? For example, I would like to create "george" down below.

Player name.toClassName = new Player();

One solution I found would be:

if(name == "george"){
    Player george = new Player();
}
if(name == "joel"){
    Player joel = new Player();
}
if(name == "kate"){
    Player kate = new Player();
} ...

But this above just looks stupid. (By the way I already have a private name; inside the Player class, I am assigning those Strings to them after the instances are made, I can get and set them etc. but that is not what I would like to manipulate with here. I'm just interested if you could make new instances this way.

13
  • 3
    create a constructor with name public Player(String name){this.name = name;} then you can create Player george = new Player("george");... Commented Jun 28, 2018 at 20:18
  • You should use a HashMap. Commented Jun 28, 2018 at 20:19
  • 2
    what is the benefit of this? how could you ever call variables with names you do not know of yet? Commented Jun 28, 2018 at 20:19
  • 3
    Possible duplicate of Assigning variables with dynamic names in Java Commented Jun 28, 2018 at 20:19
  • 2
    You don't want this. The variable name is there to let you refer to it in the code. It doesn't need to (and should not) reflect user input. Commented Jun 28, 2018 at 20:23

2 Answers 2

1

I don't like the idea of creating the variable name based on the player's name like you mentioned in your question. If you ever need to know the name of the Player then you would just access the name variable. But this code will allow you to set the Player name field when the object is created by using a constructor.

 public class Player{

     Private string name;

     Player(String name)
      {
        this.name - name;
      }

}

Player player= new Player("joel");
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I see what you mean. And do you think there could be a situation where it is still useful? Like if I had a big list of 100.000 names. Then I could find "joel" by using getPlayerName() for example. If the name is correct and it is "joel", then my program would do some calculations to it. But what if "joel" is the 79.544th in that big list of names? Wouldn't it save time and be easier if I knew I want to do something with "joel"? Or isn't that how it works? I'm really new, and this seems logical now to me, I'm trying to understand how coding works. Thank you
Variable names are for your benefit while you are coding. Once the program is running, the unique variable names based on a person's name are no longer help you. If you had a list of Players, you will have to perform a search and /or iterate through the list in order to find the entries that you are looking for anyways. When a program is in execution, you are never going to base an if statement or a loop on a variable name. The decision will be based on the value inside the variable.
1

Your variable name shouldn't have anything to do with the instance it represents. Your programming should be abstract enough so that if you changed that txt file, you don't have to change any code.

First, your Player class should accept a name in the constructor (there are tons of ways to do this, this is just the simplest).

public class Player {
    private String name;

    public Player(String inName) {
        name = inName
    }
}

Then when you create your players, don't use the names as the name for the variable.

Player player = new Player(name);

However you're reading the .txt, you can create one Player for each line, or something like that.

1 Comment

Yes that is exactly what I want to do, each line should represent a Player. Thank you for your explanation

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.