Question

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

Was it helpful?

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.

OTHER TIPS

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."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top