简体   繁体   English

通过另一个类更改静态变量

[英]change static variable through another class

i have a problem with static variable I have 2 classes: 我有一个静态变量问题,我有2个类别:

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. 问题是当我运行Test2来执行循环,然后运行Test3来终止Test2的循环时,但它不起作用。

How can I change static variable of Test2 through Test3? 如何通过Test3更改Test2的静态变量?

When you write in Test3: Test2.bool = false; 当您在Test3中编写时: Test2.bool = false; , actually you call another instance. ,实际上您调用了另一个实例。

You have 2 main methods in your code, therefore you create 2 different applications. 您的代码中有2个main方法,因此您创建了2个不同的应用程序。

And sure the flag doesn't change. 并确保标志不会改变。

You are running two different java processes with separate memory spaces. 您正在运行两个具有单独内存空间的不同Java进程。 This means that your classes are loaded in separate memory areas, one for each jvm process. 这意味着您的类将装入单独的内存区域,每个jvm进程一个。 Therefore, when accessing Test2.bool from your Test3 example you are actually referring to a different memory area than your Test2 example. 因此,从Test3示例访问Test2.bool时,实际上是指与Test2示例不同的内存区域。

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. 确保将布尔变量声明为final,以确保在多处理器环境中具有适当的可见性。

Both threads will run in the same jvm process and therefore have access to the same memory area. 两个线程将在相同的jvm进程中运行,因此可以访问相同的内存区域。

These are two different executions, you should run the two main methods in the same execution to keep Test2 bool's value: 这是两个不同的执行,您应该在同一执行中运行两个主要方法以保持Test2的值:

public class Test3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test2.bool = false;
        Test2.main(args);
    }

}

i think you can create shareClass 我认为你可以创建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
}

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; 通常,您想隐藏外部的内部字段,并通过getter / setter使其可用; since you wrote the setBool function, make bool to be private static and also add a getter: 自从您编写了setBool函数以来,将bool设为private static并添加了一个吸气剂:

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. 如果您的类在同一应用程序中运行,则可能要使用并发和ThreadsTest2Test3之间进行通信,具体取决于您的项目。 If it's not the case, then you should use Sockets or a shared memory mechanism. 如果不是这种情况,则应使用Sockets或共享内存机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM