In Java can I keep a variable from one method to a next, and across Classes?
I am trying to get a variable from Commands, modify it in QandA and expect it to persist until i modify it again.
public class Commands
{
int torch = 1;
}
_____________
public class QandA
{
Dostuff d = new Dostuff
Commands a = new Commands();
public void torch
{
System.out.println("Torch = " + torch);
a.torch = 2;
System.out.println("Torch = " + torch);
d.dostuff();
}
public class dostuff
{
public void dostuff()
{
// User imput is gathered here etc etc.
QandA();
}
}
So I would expect output to be (a loop of)
Torch = 1
Torch = 2
Torch = 2
Torch = 2
Torch = 2
Torch = 2
After 3 Cycles. But what it does is.
Torch = 1
Torch = 2
Torch = 1
Torch = 2
Torch = 1
Torch = 2
After three cycles.
Please help.