0

I'm trying to find out how to make separate toString methods based on an overloaded constructor. Take the below code for example:

public class Employee {
     private double salary;
     private String name;

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

     public Employee(String name, int salary) {
          this.name = name;
          this.salary = salary;
     }

}

Now, I would like to implement a toString method that is dependent on the object created and output the corresponding values.(i.e. one that outputs just name, the other that outputs name and salary) Do I need only one toString method and need to add an if-else statement?

Sorry if this is a silly question, I'm just learning the ropes of Java.

thebighoncho
  • 385
  • 2
  • 6
  • 20
  • 3
    Methods have no relation to the constructor that was used to create the object they were invoked on. – Sotirios Delimanolis Dec 21 '13 at 00:52
  • the toString() method should just display a value of 0 for the salary when the name only constructor is used. The toString() implementation should NOT be dependent on the constructor used. – camickr Dec 21 '13 at 00:53
  • In a general case, you could declare an `enum` class and a private (probably `final`) class member that indicates what constructor you've used, and set it in each constructor; then `toString()` can `switch` on it. That may be too heavy a solution for a simple problem like this. – ajb Dec 21 '13 at 01:53
  • ... or add a polymorphic method to the `enum` class itself, and then you wouldn't even have to use a `switch`. – ajb Dec 21 '13 at 05:04

3 Answers3

0

You can only have one toString(), but you can determine the object's "state" and put together the string accordingly. For instance, you can have a salary of -1 indicate that the first constructor was called. Therefore, your toString() would look like:

@Override
public String toString() {
    if (salary < 0) {
        ...
    } else {
        ...
    }
}

Don't forget to set salary to -1 in the first constructor.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • It's best to avoid "magic numbers"; if you do this, best to add something like `private static final double NO_SALARY = -1`, and use `NO_SALARY` instead of `-1` in the rest of the class. Also, it's not good practice to use `double` for money amounts. – ajb Dec 21 '13 at 01:56
  • @ajb I don't think `-1` qualifies as a "magic number" that requires it's own variable. As for `double`: you're right, but this is not relevant to the OP's question. – arshajii Dec 21 '13 at 01:58
  • @arshaji It's a special value that means something other than that numeric value itself (i.e. nobody has a salary of $-1 as if they have to pay a dollar to their employer every month). That's my criterion for calling it a "magic number". – ajb Dec 21 '13 at 02:03
0

First, make your salary a negative double (hopefully no one pays to work)... then, default your name field to null. Finally, check for nulls (or negative) in toString(). So, something like this -

private double salary = -1;
private String name = null;

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

public Employee(String name, double salary) { // <-- salary isn't an int.
  this.name = name;
  this.salary = salary;
}

public String toString() {
  StringBuilder sb = new StringBuilder();
  if (name != null) {
    sb.append(name);
    if (salary > 0) { // = only if both are valid.
      sb.append(" = ");
    }
  }
  if (salary > 0) {
    sb.append(salary);
  }
  return sb.toString();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Using the boxed `Double`... eww, that's awful for performance and completely unnecessary. – Kevin Ji Dec 21 '13 at 00:58
  • @arshajii May not be acceptable to business. End users are picky. – Elliott Frisch Dec 21 '13 at 00:59
  • @ElliottFrisch See, for instance, [Why do people still use primitive types in Java?](http://stackoverflow.com/questions/5199359/why-do-people-still-use-primitive-types-in-java). Also, boxed variables cannot be tested with normal equality operators, because `new Integer(2) != new Integer(2)`. – Kevin Ji Dec 21 '13 at 01:03
0

Well what you are doing here is just instantiating constructors, which has nothing to do with any methods. Constructors just help you initialize and sort of provide context to variables that you will be working with.

If as you said, printing or output-ing the salary and name, you would have to create methods like:

public String printName(){
       return name;
}

public String printAll(){
       return name + "" + String.valueOf(salary);
}
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74