Reputation: 3
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=***}
Upvotes: 0
Views: 733
Reputation: 36
Objects exist independently of any variables that reference them. You can't use their variable name in a toString()
or any other method.
Upvotes: 1
Reputation:
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 }";
Upvotes: 0