-4

What I should change to print the name of chair, which is chairNumber1?

public class Employee {                                                                                         
    private Chair s;                                                                                            

    Employee(Chair s) {                                                                                         
        this.s = s;                                                                                             
    }                                                                                                           

    void showData() {                                                                                           
        System.out.println("Name of chair : " + s);                                                             
    }                                                                                                           
}                                                                                                               

public class Chair {                                                                                            

}                                                                                                               

public class Hlavna {                                                                                           
    public static void main(String[] args) {                                                                    
        Chair s = new Chair("chairNumber1");                                                                    
        Employee c1 = new Employee(s);                                                                          
        c1.showData();                                                                                          
    }                                                                                                           
}  

Why when I want to print name of the Chair, which is chairNumber1, Java prints on console the address of chairNumber1, but not it's name?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Hihaha
  • 13
  • 5

4 Answers4

1

You must be already aware of the fact that every class in Java inherits a class called Object by default. This class has a method toString() which returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

When you use System.out.println("Name of chair : " + s);, it will call s.toString() but since you haven't provided your own implementation of toString() inside class Chair, it will call the toString() method of class Object which is the default superclass of class Chair. This is why you see the value which you think as the address of chairNumber1.

To get your desired String, you need to override the toString() method something like:

public class Chair {
    private String name;
    public Chair(String name) {
        this.name = name;
    }
    public String toString() {
        return name;
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

There are a couple of things going on here.

You have created a chair object in your main method of your Hlavna class. To this Chair object you have provided an argument, although from the code above Chair does not take an argument.

In the same way that you have made the Employee class take an argument of chair, you should take the Chair take an argument of name, like so:

public class Chair
{
    private String name;

    Chair(String chairName)
    {
        this.name = chairName;
    }
}

Now this isn't enough. When you print any Java object, under the hood what is really happening is the object's toString method is called. By default this prints the object's address, but you can override that by implementing the method yourself, like so:

public class Chair
{
    private String name;

    Chair(String chairName)
    {
        this.name = chairName;
    }

    public String toString()
    {
        return this.name;
    }
}

Now, when you print a chair object it will call the Chair object's implementation of toString, which here returns the chair's name.

Your employee class is correctly printing the "toString()" method of the chair that you pass to it as you construct it, but currently that looks like an address. If you change the Chair object to the above code, that will instead print the chair name, which is what you are after.

The full code would look like this:

public class Employee    
{
    private Chair s;

    Employee(Chair s)
    {
        this.s = s;
    }

    void showData()    
    {   
        System.out.println("Name of chair : " + s);   
    }
}

public class Chair
{
    private String name;

    Chair(String chairName)
    {
        this.name = chairName;
    }

    public String toString()
    {
        return this.name;
    }
}


public class Hlavna
{
    public static void main(String[] args)
    {
        Chair s = new Chair("chairNumber1");
        Employee c1 = new Employee(s);
        c1.showData();
    }
}
Jack Schofield
  • 302
  • 1
  • 15
0

define a method inside your chair class that returns the name or override the toString method.

example:

public class Chair{

  private String chairName;

  Chair(String chairName){
    this.chairName = chairName;
  }

  public String toString(){
    return chairName;
  }
}

now inside showdata() call toString():

void showData(){
  System.out.println("Name of chair : " + s.toString());
}
-1
public class Chair {
    private String name;

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

    @Override
    String toString() {
        return name;
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Goblinator
  • 11
  • 5
  • You should add some explanation of your code to help the OP understand why it solves his problem. Also, I don't see how your answer adds anything that isn't already provided in the other answers to this question. – Abra Nov 16 '19 at 06:05