Junior — Middle
Kuidas saab programmi praegust Instance objekti kuvada või vaadata?
sobes.tech AI
Vastus AI-lt
In Java, to output the current object (instance) from within a class, you typically override the toString() method. This allows you to see information about the object when calling System.out.println(this).
Example:
public class MyClass {
private int id;
private String name;
public MyClass(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "MyClass{id=" + id + ", name='" + name + "'}";
}
public void printInstance() {
System.out.println(this); // Will display information about the object
}
}
// Usage:
MyClass obj = new MyClass(1, "Test");
obj.printInstance();
If you want to see the object during debugging, you can use your IDE's debugger, which will show all fields of the current instance.