How do I print the variable name holding an object?

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

    import java.lang.reflect.*;
    
    public class myclass {
        private myclass ob;
    
        public static void main(String args[]) {
                Field fd[] = myclass.class.getDeclaredFields();
                for (int i = 0; i < fd.length; i++) {
                      System.out.println(fd[i].getName());
                }
        }
    }
    

提交回复
热议问题