Is it possible to access a public variable in a class while if the only available access to that class is its .class.
As an example if I have A_VARIABLE in both ClassA and ClassB, if I put the reference to each class Object into an HashSet, am I able to retrieve the A_VARIABLE for each item in the HashSet.
public class ClassA{
public static final String A_VARIABLE = "ABC";
}
public class ClassB{
public static final String A_VARIABLE = "123";
}
public class ClassC{
private void someMethod(){
HashSet<Class> someClassHashSet = new HashSet<>();
someClassHashSet.add(ClassA.class);
someClassHashSet.add(ClassB.class);
for (Class someClass : someClassHashSet){
System.out.println("The value is: " + /*someClass.A_VARIABLE*/);
}
}
}
Which I would like to print:
The Value is: ABC
The Value is: 123
ClassA.A_VARIABLE(it's a constant field) and anequalson the class should do the trick