質問

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

役に立ちましたか?

解決

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.

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top