Lets say I have 3 Classes: A
, Data
, and B
I pass a variable from class A
which sets that passed variable to a private variable in class Data
.
Then in class B
, I want to call that specific variable which has been changed.
So I do
Data data = new Data();
data.getVariable();
It will then return null, since in class Data
I initialize variables to nothing (ex: int v;
), and I think that class B
is initializing a brand new class and resetting the values to default, but I don't know how to fix this.
I know that the variable is setting properly because in class A
if I do data.getVariable()
it will print the variable that was set.
Class A
:
Data data = new Data();
int d = 1;
data.setVariable(d);
Class Data
:
private static int b;
public void setVariable(int s)
{
b = s;
}
public int getVariable()
{
return b;
}
Class B
:
Data data = new Data();
private int v;
v = data.getVariable();
System.out.println(v);
This will print out 0 instead of the actual value