I don't think you fully appreciate the definition of static final...
I do fully appreciate what those qualifiers mean. I now can see how
you got confused. Ok, let's take the case
where I know this FQN at compile time. I could then do this:
Object myObj = com.whatever.mycompany.MyConstants.MY_CONST;
This is perfectly valid and trivial
Now say I only have the above as a string representing the fully
qualified name?
I could do:
Object myObj = null;
Class c = Class.forName("com.whatever.mycompany.MyConstants");
Field f = c.getField("MY_CONST");
myObject = f.get(null);
But that would be only possible if I knew that Containing class and
the member I needed.
1. Why do you need to use reflection?
Do you know of a way to access member fields at runtime without
knowing their names?
2. From where is the String given to you?
A database? XML? Commandline? Does it matter?
3. Why can the String given to you represent a field?
I assume you mean "can't" -- and indeed it does or may
4. Why do you need to reassign a static final variable?
I don't, miscommunication issue. See above
If you still desperately want to do this, note that of any identifier,
the simple name of the class will either appear as the rightmost, second
from the right, are not at all (see the JLS). It should become clear how
to proceed...
If I assume that the string passed to me is a member field, I can do
that above reflection. But, such a String could represent a fully
qualified class name. At which point moving back one '.' would put me
at the package level. Can I determine if a FQN is a Class or a Field
in a Class? I know that, when referring to inner classes they are
"com.blah.MyClass$Inner" -- but this isn't an inner class.
I suppose I need a concrete example to really show people that despite
honest bonifide credentials on this subject I am not a rookie. So,
Here goes
public class MyConstants {
public static final SomeInterface CONST1 = new SomeSubtype() {
public void interfaceMethod() {}
};
public static final SomeInterface CONST2 = new SomeSubtype() {
public void interfaceMethod() {}
};
public static final SomeInterface CONST3 = new SomeSubtype() {
public void interfaceMethod() {}
};
}
SomeInterface x = MyConstants.CONST1; // trivial.
SomeInterface X = ..? // do the above given only this string:
"MyConsts.CONST1"; // This string is passed at runtime