-1

I'm pretty new to Java so I don't know if my question make any sense in the first place. But I will try to describe it as best as I can.

How do I return the name of an object? So I made a class called Player and made the following object:

Player John = new Player();
System.out.println(John);

How do I make it so that it prints/returns the word "John" instead of anything else? I would appreciate any help, thanks a lot!

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
Niko H
  • 53
  • 1
  • 9
  • Possible duplicate of [Java Reflection: How to get the name of a variable?](https://stackoverflow.com/questions/744226/java-reflection-how-to-get-the-name-of-a-variable) – Srini Mar 22 '19 at 21:56
  • Possible duplicate of [How do I print the variable name holding an object?](https://stackoverflow.com/questions/3959206/how-do-i-print-the-variable-name-holding-an-object) –  Mar 22 '19 at 22:12
  • Possible duplicate of [Java—how can I dynamically reference an object's property?](https://stackoverflow.com/questions/13128194/java-how-can-i-dynamically-reference-an-objects-property) – Michał Ziober Mar 22 '19 at 23:00

3 Answers3

5

In your example you are not actually setting the name of the player to John you are creating an object called John.

To fix this declare a field inside your Player class:

public class Player {
   String name;

   public Player(String name) {
       this.name = name;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
}

Now you can create as many players as you want and name them:

Player player = new Player("John");

When you want to get a player's name, just call the getName() function.
When you want to set a player's name, just call the setName() method.

To print the players name on the console use this:

System.out.println(player.getName());

Or you can override the toString() method of Player to offer a textual representation of the object, in this case only a player's name:

@Override
public String toString() {
    return "Player[name=" + name + "]";
}

Then, you can use:

System.out.println(player); // toString() is called if player != null
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
0

Have a instance variable which holds the name property and populate it at the time of object instantiation. Override "toString()" method to display the name -

@Override
 Public String toString(){
     return name;
  }

  Player John = new Player();
  System.out.println(John);
Rahul Vedpathak
  • 1,346
  • 3
  • 16
  • 30
0

First off, you should create a constructor for the class to accept a string to set the name of the player in the class.

You can then override the toString() method so the string representation of the class displays the name of the player or you can make a getter method to retrieve the name of the player.

Example:

public class Player {

    private String name;

    public Player(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

You can use it as such:

public class App {
    public static void main(String[] args) {
        // Using constructor to set name of the player
        Player john = new Player("John");

        // You can then print the name of the player by printing the object
        // directly or by calling its getter method
        System.out.println(john); // prints "John"
        System.out.println(john.getName()); // also prints "John"
    }
}
Justifyz
  • 21
  • 3