I have this problem where I set the value for some variables from a jForm to a class where I store the variables. Set the values, Ok, get the values, Ok. But when I go to a different jForm and call the getters it returns empty values. I don't know what is wrong with my code and would really apreciate if someone could help me on this one.
The code is pretty straigth-forward
public class Variables
{
private int Var1;
private int Var2;
private double Var3;
private int Var4;
public int getVar1() {
return capacidad;
}
public void setVar1(int Var1) {
this.Var1 = Var1;
}
public int getVar2() {
return Var2;
}
public void setVar2(int Var2) {
this.Var2 = Var2;
}
public double getVar3() {
return Var3;
}
public void setVar3(double Var3) {
this.Var3 = Var3;
}
public int getVar4() {
return Var4;
}
public void setVar4(int Var4) {
this.Var4 = Var4;
}
}
For example, I set all the values calling the functions from a jForm like this:
variables.setVar1(value1);
variables.setVar2(value2);
variables.setVar3(value3);
variables.setVar4(value4);
Then I try to get the values from a different jForm and they are all 0s
(edit)
I have already tried to call them from the same jForm where I set them and works just fine
(editedit)
jForm1 code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int value1;
int value2;
double value3;
int value4;
try
{
value1 = (int) 1Spinner.getValue();
value2 = (int) 2Spinner.getValue();
value3 = (int) 3Spinner.getValue();
value4 = (int) 4Spinner.getValue();
if(value1>0)
{
variables.setVar1(value1);
variables.setVar2(value2;
variables.setVar3(value3);
variables.setVar4(value4);
}
else
{
}
}
catch(Exception e)
{
System.out.orintln("Error");
}
}
jForm2 code:
int value1;
int value2;
int value3;
int value4;
Variables variables = new Variables();
value1 = variables.getVar1();
//and so on, all of them return empty
The following code in jForm2 creates a new instance of the variables
variable. So any instance members are going to be uninitialized.
Variables variables = new Variables();
if you want it to have the same values from jForm1, you need to provide a way for jForm2 to get them from jForm1, like so:
// in jForm1
private static Variables variables = new Variables();
public static getVariables() {
return variables;
}
// in jForm2
Variables variables = jForm1.getVariables();
OR you can declare your Variables
instance members to be static like so:
public class Variables {
private static int Var1;
private static int Var2;
private static double Var3;
private static int Var4;