How do I print the variable name holding an object?

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

    Objects don't have names, unless you happen to be using a class which allows each object to be given one (e.g. via a variable retrieved with getName()).

    In particular, the name of any particular variable used to refer to an object is completely unknown to the object itself. So you can't do:

    Object foo = new Object();
    // There's no support for this
    String name = foo.getName(); // expecting to get "foo"
    

    (Bear in mind that several variables could all refer to the same object, and there don't have to be any named variables referring to an object.)

    0 讨论(0)
  • 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().

    0 讨论(0)
  • 2020-12-16 15:38

    You can't get the name of object reference. Instead you can get its hashcode or counter. I solve this problem as follows:

    class ObjectName
    {
        static int num;
        ObjectName()
        {
         num++;
        }
    
        public int getObjectCount()
        {
            return num;
        }
        public static void main(String[] args) {
            ObjectName ob1=new ObjectName();
            System.out.println(ob1.getClass().getName()+" "+ob1.getObjectCount());
            ObjectName ob2=new ObjectName();
            System.out.println(ob1.getClass().getName()+" "+ob1.getObjectCount());
        }
    }
    

    Output:

    ObjectName 1
    ObjectName 2
    
    0 讨论(0)
  • 2020-12-16 15:39

    To print the object type name:

    System.out.println(myObject.getClass().getName());
    
    0 讨论(0)
  • 2020-12-16 15:44

    Workaround. If you want the object name then you can initialize at constructor.

    //in class myClass
    
    private String objName;
    
    myClass(String objN)
    {
        this.objName = objN;
    ..
    }
    public String getObjName() {
        return objName;
    }
    //in main()
    .
    .
    .
    myclass ob = new myclass("ob"); //any other variables accessing this object 
                                  //eg. temOb = ob get the same object name "ob"
    System.out.println("object name is: " + ob.getObjName());
    myclass ob1 = new myclass("ob1");
    System.out.println("object name is: " + ob1.getObjName());
    
    0 讨论(0)
  • 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().

    0 讨论(0)
提交回复
热议问题