Вопрос

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