Reputation: 123
I created 5 classes with static variables having the same name.
class A
{
public static int a;
//other vatiables
}
class B
{
public static int a;
//other variables
}
....and so on.
Now what i want to do is access these static variables using the same identifier to reduce the ambiguity.
like
void printAll(String className)
{
//prints all the variables of className
}
So i want use this one method only to print the values of any particular class.
Is there any way of doing so without reflection.??
Upvotes: 0
Views: 519
Reputation: 131496
Is there any way of doing so without reflection.??
With fields that have a static
modifier, you don't have other ways.
To be able to do that without reflection you should use polymorphism feature.
But that is available only with instance methods.
As workaround you could define a int getA()
instance method that relies on the static
field and to allow to invoke the method on any class that have the a
field, define this instance method in an interface.
Interface :
public interface CommonData{
int getA();
}
Class A :
class A implements CommonData {
public static int a;
public int getA(){
return int a;
}
}
Class B :
class B implements CommonData {
public static int a;
public int getA(){
return int a;
}
}
Upvotes: 1