Reputation: 480
i have a problem with static variable I have 2 classes:
public class Test2 {
public static boolean bool;
public static void main(String[] args) {
// TODO Auto-generated method stub
bool = true;
int run=0;
while (bool==true) {
System.out.println("Test 2 "+run);
System.out.println(bool);
run++;
}
}
public static void setBool(boolean temp){
bool = temp;
}
}
and
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test2.bool = false;
}
}
The problem is when I run Test2 to perform the loop, and then I run Test3 to terminate the loop of Test2 but it doesn't work.
How can I change static variable of Test2 through Test3?
Upvotes: 1
Views: 1869
Reputation: 1597
There are a couple of shortcomings in your code so I will try to give you some hints.
Usually you want to hide internal fields from outside world and made them available through getters/setters; since you wrote the setBool
function, make bool
to be private static
and also add a getter:
private static boolean bool;
public static boolean getBool(){
return bool;
}
public static void setBool(boolean temp){
bool = temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
bool = true;
int run = 0;
while (bool == true) {
System.out.println("Test 2 " + run);
System.out.println(bool);
run++;
}
}
Depending on your project, you might want to use concurrency and Threads
to communicate between Test2
and Test3
if your classes run in the same application. If it's not the case, then you should use Sockets
or a shared memory mechanism.
Upvotes: 0
Reputation: 2322
You are running two different java processes with separate memory spaces. This means that your classes are loaded in separate memory areas, one for each jvm process. Therefore, when accessing Test2.bool from your Test3 example you are actually referring to a different memory area than your Test2 example.
I suspect that what you heed here is two separate threads:
class ThreadA extends Thread {
private final boolean running = true;
public void run() {
while(running) {
doStuff();
}
}
public void kill() {
running = false
}
private void doStuff() {
// do some interesting stuff
}
}
class ThreadB extends Thread {
private ThreadA thread;
public ThreadB(ThreadA aThread) {
thread = aThread;
}
public void run() {
// on some condition
thread.kill();
}
}
class Runner {
public static void main(String[] args) {
ThreadA t1 = new ThreadA();
ThreadB t2 = new ThreadB(t1);
t1.start();
t2.start();
}
}
Make sure that you declare your boolean variable as final in order to guarantee proper visibility in a muti-processor environment.
Both threads will run in the same jvm process and therefore have access to the same memory area.
Upvotes: 1
Reputation: 876
i think you can create shareClass
class shareMe {
public static static boolean bool;
}
class some1 {
//USE THE ABOVE VARIABLE BY shareMe CLASS NAME
}
class some2 {
//USE THE ABOVE VARIABLE BY shareMe CLASS NAME
}
Upvotes: 0
Reputation: 4511
These are two different executions, you should run the two main methods in the same execution to keep Test2 bool's value:
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test2.bool = false;
Test2.main(args);
}
}
Upvotes: 0
Reputation: 77930
When you write in Test3: Test2.bool = false;
, actually you call another instance.
You have 2 main
methods in your code, therefore you create 2 different applications.
And sure the flag doesn't change.
Upvotes: 8