How do I print the variable name holding an object?

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

    Slightly more complete (but essentially the same as above):

    1. If you have an object: object.getClass().getName() will give you the fully qualified name of the object (i.e.package.className. For example, String thing = new String(); thing.getClass().getName() will return "java.lang.String".
    2. If you have a class name: className.class.getName() will give you the fully qualified name of the object (i.e.package.className. For example. String.class.getName() will return "java.lang.String".

    Print it how ever you want. Perhaps using System.out.println().

    The name of the variable is compile time information that is not typically stored after compilation. The variable name is stored if you compile with debugging information.

    In mamy contexts, it is a ridiculous request to want to print the name of a variable, but if you really need to do that, read up on java debugging information.

    0 讨论(0)
  • 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());
                }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 15:55

    Try reflection + hash code. E.g.:

    String hashcode = ob.hashCode();
    String obName;
    java.lang.reflect.Field[] fields = 
    ob.getClass().getDeclaredFields();
    
    for( final  java.lang.reflect.Field field : fields )
    {
      if( field.hashCode().equals(hashcode) )
      {
        obName = field.getName();
        break;
      }
    }
    System.out.println(obName);
    
    0 讨论(0)
  • 2020-12-16 15:56

    This achieves the required result, but it is a bit of a fudge, in that if the string returned by toString() is to actually reflect the name of your variable (which of course, only you, as the coder will know), you have to use the convention that the method uses i.e. in this implementation, because the returned string is in the format ClassName+counter, you have to name your variable using that format: myClass1, myClass2, etc. If you name your variable in some other way (e.g. className+A/B/C etc or someRandomVariableName) although the return string would remain as implemented by the toString() method i.e. returning myClass1, myClass2, etc, it wouldn't reflect the variable name you have actually used in your code.

    class myClass {
    
        private static int counter = 1;
        private String string;
        private int objCounter;
    
        myClass(String string) {
            this.string = new String(string);
            objCounter = counter;
            counter++;
        }
    
        @Override
        public String toString() {
            return this.getClass().getName() + objCounter + ": " + this.string;
        }
    
        public static void main(String[] args) {
            myClass myClass1 = new myClass("This is the text for the first object.");
            myClass myClass2 = new myClass("This is the text for the second object.");
            System.out.println(myClass1);
            System.out.println(myClass2);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题