'How to print the address of an object if you have redefined toString method
I'm a newbie to Java. Now I'm studying equals and == and redefinition of equals and toString.
I would like to use both the toString method that I have redefied and the default method that is inherited from the Object class.
I failed to use that super modificator to reach that method.
This is for educational purposes only. What I would like to get is more clear if you will have a look at the comments in my code.
Could you help me here?
My code is:
public class EqualTest{
public static void main(String[] args){
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice1);
Employee alice2 = alice1;
//System.out.super.println(alice2);
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice3);
System.out.println("alice1==alice2: " + (alice1==alice2));
System.out.println("alice1 == alice3: " + (alice1==alice3));
System.out.println("alice1.equals(alice3): " + alice1.equals(alice3));
}
}
class Employee{
...
public String toString(){
return getClass().getName() + "[name = " + name +
", salary=" + salary + ", hireDay=" + hireDay + "]";
}
}
Solution 1:[1]
If you want to achieve sort-of default toString() behavior, you can make use of System.identityHashCode() method. Default toString() will then look like this:
public String toString(Object o) {
return o.getClass().getName() + "@" +
Integer.toHexString(System.identityHashCode(o));
}
Solution 2:[2]
You can call super() method to execute the corresponding superclass method.
class Employee{
...
public String toString(){
String s = super.toString();
return getClass().getName() + "[name = " + name +
", salary=" + salary + ", hireDay=" + hireDay + "]" + s;
}
toString() in Object class is as follows
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Solution 3:[3]
Here is an in-depth answer about overriding equals and hashcode
What issues should be considered when overriding equals and hashCode in Java?
The key point being The relation between the two methods is:
Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
Solution 4:[4]
You may create another method inside your Employee class to use super toString method.
See example:
public class Employee {
public String toString() {
return "MyToStringMethod";
}
public String superToString() {
return super.toString();
}
public static void main(String[] args) {
Employee b = new Employee();
System.out.println(b);
System.out.println(b.superToString());
}
}
or combine both in one method:
public class Employee {
public String toString() {
return super.toString() + " MyToStringMethod";
}
public static void main(String[] args) {
Employee b = new Employee();
System.out.println(b);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew Logvinov |
| Solution 2 | |
| Solution 3 | Community |
| Solution 4 | Alexey Odintsov |
