I have created an object
Student student1 = new Student(***, ***, ***, ***);
In my toString override, how do I pull/reference the object's identifier student1? Without creating a field for it.
@Override
public String toString() {
return "Student{"
"id=" this.id
'}';
}
System.out.println(student1)
I want the output to be Student{student1, id=***}
CodePudding user response:
Just create a student object and call the toString()
method on the student object.
For eg,
Student student1 = new Student(***, ***, ***, ***);
student1.toString();
This will call your override method toString()
in your Student class.
In your toString method on Student class, you don't need stdout or any output.
Just return "Student { "id =" this.id }";
CodePudding user response:
Objects exist independently of any variables that reference them. You can't use variable name in toString() method.