Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Calling a variable from another class using a variable for the class name in Java?
Hi there,
I am creating a java program.
I would like to call a variable from a class ex. "Class1"
Let's say the variable is called "Variable1"
And set an array value equal to it
So the way I would do this is:
anArray[1] = Class1.Variable1
This works well, but I am going to need to get Variable from multiple different classes depending on which class the user desires. So for instance there will be a Variable1 in Class2 and Class3 and Class4 etc. to be called if the user wants that class.
Is there any way I can set a variable like x and say
x = Class1
and then call
x.Variable1
You may be asking yourself why in the world would i want to do this, but it is because there are 33 different variable i need to call. Variable1, 2, 3 all the way to 33. So if i could just type in the class i want for that particular run that would be desirable. I'm thinking it should look something like this...
anArray[1] = x.Variable1
anArray[2] = x.Variable2
anArray[3] = x.Variable3
...
anArray[33] = x.Variable33
When i try to do this i receive an error. Does anybody know how i can accomplish such a thing?
Any help is greatly appreciated.
Many Thanks.
4 Answers
- husoskiLv 78 years ago
You can have all those classes extend a common base class with the variables in it. This is almost always poor design, though. The normal object-oriented way to accomplish what you seem to want is to define methods, not variables. These can be inherited, overridden, and can take active control over where information is stored and what changes can be made.
Still, this works:
class xyzzy {
public static String classVar1 = "default 1";
public static String classVar2 = "default 2";
public static String classVar3 = "default 3";
public String instanceVar1 = "value 1";
public String instanceVar2 = "value 2";
public String instanceVar3 = "value 3";
}
class plugh1 extends xyzzy {
public static String classVar1 = "plugh1 class";
}
class plugh2 extends xyzzy {
public String instanceVar1 = "plugh1 member";
}
Now both plugh1 and plugh2 have class (static) variables plugh1.classVar1 through 3 and each instance of new plugh1() or new plugh2() has instance variables instanceVar1 through 3.
The class (static) variables are shared, when not overridden, so plugh1.classVar2, plugh2.classVar2 and xyzzy.classVar2 all refer to the very same variable. "Set one and you set them all."
Due to the override, plugh1 has its own separate classVar1 variable, but plugh2.classVar1 and xyxxy.ClassVar1 are still the same as each other.
The instance variables aren't shared...every new object gets its own copy. However every instance of xyzzy, plugh1 or plugh2 will have public instance variables instanceVar1 through 3, and the xyzzy base class sets the common default initial value for all three classes. The versions above override the initial values of instanceVar1, but keep the base class default values for instanceVar2 and 3.
That's a lesson on how inheritance works with variables in Java. The OOP lesson is to avoid using public variables, period. Obviously you need instance variables. That's the object's state. Private class variables are handy, too. Variables in a class's public interface are brittle, though. You can't change them to anything else without breaking programs that use the class.
The "encapsulation" idea in OOP is supposed to allow changing the internal implementation of a class without affecting the users of the class. If you decided that 33 separate variables was better implemented as a single array of values, you could rewrite getVar1() and setVar1(value) methods, and even provide new getVar(n) and setVar(n, value) methods, but there's no way to declare an array classVars[] and a scalar classVar2 so that changing classVar2 automatically changes classVars[1] as well.
Unless you have a Very Good Reason to do otherwise, make "accessor" methods (also called "getter" and "setter" methods) for your class and have them read and set the internal values, as required.
I hope some of that helps...
- Anonymous8 years ago
I assume when you say class you actually mean the object that is the instance of the class.
You can use a method that takes in the class instance as its argument:
public void assignVars(YourClass obj)
{
anArray[1] = obj.Variable1;
anArray[2] = obj.Variable2;
and so on...
}
and just call this method in your main code.
- 8 years ago
Why dont you just use the same variable name in each class instead of calling them Variable1, Variable2, etc... i.e. Just call it Variable.
Realistically, this is not a very good design. I'd refactor this logic.
- daSVgrouchLv 78 years ago
need more info
a) are these variables static members in their respective classes, or are they part of the instance variables for an object of that class?
b) can the classes all be subclasses of the same class so you can use inheritance
c) have you looked at using IMPLEMENTS and a standard interface to allow these accesses to work?