Question

Is there a method that exists that allows me to get the name of a field in another object?

Était-ce utile?

La solution

If I understand your question correctly, you can (mis)use Java's Stack Trace system to determine the caller. Put something like this in the constructor of an object:

try {
        throw new Exception();
    } catch (Exception ex) {
        String s = ex.getStackTrace()[1].getClassName();
        System.out.println(s);
    }

I doub't that's EXACTLY what you are looking for, but, if you play around with it a bit, I think you'll find what you want.

Autres conseils

There is no way to do this, period. An object can be simultaneously referred to by many variables, and there is no "master list" you can query. The information you want is simply not available. Objects don't have "names."

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top