Just having a play around with Java reflection and I think I'm getting the hang of it for the most part. I understand from this question/answer that, for the most part, I'm limited to static variables. If I have an instance of the class though, I can access non-static variables, which does make sense, I get that much.
Say I have the following two classes:
public class A
{
private static int _staticInt;
public static void main(String[] args)
{
B instanceOfB = new B();
}
}
public class B
{
private int _nonStaticInt;
public Game() {}
}
I understand how to access _staticInt
, that's not an issue.
My understanding is that I can get the Field
for _nonStaticInt
in the same way (i.e. Field f = B.class.getDeclaredField("_nonStaticInt");
). From other research (javadocs, trails, etc) I have gathered that I need an instance of B
in order to get the value of _nonStaticInt
.
So my question; Since main
is static, is it possible to access instanceOfB
in order to access the value of _nonStaticInt
? I don't think it is possible, but I thought it's always best to consult people that are more knowledgable than myself before giving up on the idea.