How do I print the variable name holding an object?

前端 未结 10 1143
耶瑟儿~
耶瑟儿~ 2020-12-16 15:27

How do I print the variable name holding my object?

For example, I have:

myclass ob=new myclass()

How would I print \"ob\"?

10条回答
  •  没有蜡笔的小新
    2020-12-16 15:36

    I'm assuming you want a way to "print" an object, in which case, you'll first want to override the toString() method for your object. This will allow you to specify what an object of your type returns when you call toString().

    Then, you can simply do System.out.println(MyObject);

    This will implicitly call MyObject's toString().

提交回复
热议问题