How do I print the variable name holding an object?

前端 未结 10 1115
耶瑟儿~
耶瑟儿~ 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:49

    System.out.println(); Is the command used to print out to the console.

    So if you have your own class that you created and instantiated, you could do:

    MyObject obj = new MyObject();
    System.out.println(obj);
    

    and that would print out the toString() implementation of MyObject. The default implementation is not very interesting, so for useful info, you would have to override toString().

提交回复
热议问题