Reputation: 23
I have been having trouble accessing a variable from another class. Here are my variables:
public static byte agressivePoints = 0;
public static byte peacefullPoints = 0;
public static byte meanPoints = 0;
public static byte happyPoints = 0;
public static byte sadPoints = 0;
I put them outside of my Main class. In this class, I am tying to access those variables from the other class to put them in this one.
public class Check_Anwser {
public static void Check_Answer(int Input) {
boolean TryAgain = false;
do {
switch (Input) {
case 'a': {
++agressivePoints;
break;
}
case 'b': {
++sadPoints;
break;
}
case 'c': {
++meanPoints;
break;
}
case 'd': {
++peacefullPoints;
break;
}
case 'e': {
++happyPoints;
}
default: {
System.out.println("You have entered an invalid anwser. Please try again");
}
}
} while (TryAgain != true);
}
}
}
But when I do this, there is an error. For some reason, the variables from the first class aren't shared with the second class.
Upvotes: 1
Views: 285
Reputation: 27
The best way to do this is have a Variables class, in which holds all your variables, and inside the Correct_Answer class you could simply call
Static Variables v;
and infront of each of the variables you wish to call just do c.VARIABLENAME;
Upvotes: -1
Reputation: 17454
How do you access variables from different classes in Java?
If the variable (or member) of another class is non-static. You can create an instance(object) of that class and access to its variable, for example:
class Warrior
{
private int strength;
public int getStrength(){
return this.strength;
}
}
class TestRunner{
public static void main(String[] args){
Warrior w = new Warrior();
w.getStrength(); //Access to strength of warrior via a Warrior object
}
}
If the member is public, you will be able to access it directly like:
w.strength;
But when you learn the concept of encapsulation and data protection, you will realize generally we will try to make the instance variables as private.
As for static members, they belongs to the class and not individual objects. So it is encouraged you access them via the class name, for example:
class Warrior{
public static final int SPEED; //let say speed for all warriors are unified
}
class TestRunner{
public static void main(String[] args){
int speed = Warrior.SPEED; //Access a class variable via the class name
}
}
Upvotes: 3
Reputation: 77
To access variables from other class say A to call the variable into your main class use (instance of classA).varaibleName In your case corresponding class name of the variable say class A contains
public static byte agressivePoints = 0;
by creating an instance of the class like
A inst =new A();
++inst.agressivePoints;
Upvotes: 0
Reputation: 269627
Use the expression Main.aggressivePoints
to access these global variables.
I encourage you to keep learning about object-oriented design, so that you can understand why it will be better when you create objects and protect data like this inside them. It's called encapsulation.
Upvotes: 1