java - 从另一个类访问递增的静态变量

时间:2021-08-03 15:59:53

I have a static variable and updating it's value in class. But when i access this variable from another class , it shows unupdated value.

我有一个静态变量并在课堂上更新它的值。但是当我从另一个类访问此变量时,它显示未更新的值。

CLASS A

  public static int postID = 1;

  public static String Creator()
  {
    String message = "POST id="+postID;
    return message;
  }

  void updatePostID()
  {
      postID++; //this function is being called each 10 seconds
  }

  @Override
  public void start() { 
    handler.post(show);
  }

  Handler handler = new Handler();
  private final Runnable show = new Runnable(){
    public void run(){
        ...
               updatePostID();
               handler.postDelayed(this, 10000);    
    }
  };

CLASS B

  String message = A.Creator(); //this always prints postID as 1 all time 

I need a global variable that i can access from each class and update its value. Waiting for your help (I am using this with a Android Service)

我需要一个全局变量,我可以从每个类访问并更新其值。等待你的帮助(我使用Android服务)

4 个解决方案

#1


2  

this is a tested code .

这是经过测试的代码。

public class A {

    public static int id = 0;

    public static int increment(){
        return A.id++;
    }

}

public class B {

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println(A.increment());
        }

    }
}

#2


1  

class A { static int id=0;

class A {static int id = 0;

//I am updating id in my function ,
{
  id++;
 }
}

public class StartingPoint {

public class StartingPoint {

public static void main(String... args){

    A a = new A();
    A b = new A();

    System.out.println(A.id);
    System.out.println(a.id);
}

}

#3


1  

You need to call work to execute id++;

你需要调用工作来执行id ++;

class B {

    public static void main(String... args){

        A a = new A();
        a.work(); // You need to call it to apply add operation

        System.out.println(A.id); // Prints 1

    }

}

And this is a sample class A:

这是A类的示例:

class A {

    static int id = 0;

    public void work(){

        id++;

    }
}

Save class A in a file named A.java and class B in a file named B.java.

将类A保存在名为A.java的文件中,将类B保存在名为B.java的文件中。

Then compile B. Since B creates an instance of class A, A will be compiled and you don't need to compile A separately-

然后编译B.由于B创建了A类的实例,因此将编译A并且您不需要单独编译A

javac B.java

After compilation, to execute/run-

编译后,执行/运行 -

java B

#4


0  

Sajal Dutta's answer explains it perfectly, but if you want to keep it ALL static (i.e. not create any objects of class A, you could modify the code slightly to this:

Sajal Dutta的答案完美地解释了它,但如果你想保持它静态(即不创建任何A类对象,你可以稍微修改代码:

class A {
    static int id = 0;
    public static void work(){
        id++;
    }
}

Then:

class B {
    public static void main(String[] args){
        System.out.println(A.id);
        A.work();
        System.out.println(A.id);
    }
}

This would produce:

这会产生:

0
1

Edit (with regard to your updated question)

编辑(关于您更新的问题)

Where are you specifying the update of the static int? From the code you've provided all you will do is print out the same int over and over as the method containing the increment process is never called.

你在哪里指定static int的更新?从你提供的代码中你要做的就是一遍又一遍地打印出相同的int,因为永远不会调用包含增量过程的方法。

Edit 2:

Try this:

Change:

handler.post(show);

to:

handler.postDelayed(show, 10000);

#1


2  

this is a tested code .

这是经过测试的代码。

public class A {

    public static int id = 0;

    public static int increment(){
        return A.id++;
    }

}

public class B {

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println(A.increment());
        }

    }
}

#2


1  

class A { static int id=0;

class A {static int id = 0;

//I am updating id in my function ,
{
  id++;
 }
}

public class StartingPoint {

public class StartingPoint {

public static void main(String... args){

    A a = new A();
    A b = new A();

    System.out.println(A.id);
    System.out.println(a.id);
}

}

#3


1  

You need to call work to execute id++;

你需要调用工作来执行id ++;

class B {

    public static void main(String... args){

        A a = new A();
        a.work(); // You need to call it to apply add operation

        System.out.println(A.id); // Prints 1

    }

}

And this is a sample class A:

这是A类的示例:

class A {

    static int id = 0;

    public void work(){

        id++;

    }
}

Save class A in a file named A.java and class B in a file named B.java.

将类A保存在名为A.java的文件中,将类B保存在名为B.java的文件中。

Then compile B. Since B creates an instance of class A, A will be compiled and you don't need to compile A separately-

然后编译B.由于B创建了A类的实例,因此将编译A并且您不需要单独编译A

javac B.java

After compilation, to execute/run-

编译后,执行/运行 -

java B

#4


0  

Sajal Dutta's answer explains it perfectly, but if you want to keep it ALL static (i.e. not create any objects of class A, you could modify the code slightly to this:

Sajal Dutta的答案完美地解释了它,但如果你想保持它静态(即不创建任何A类对象,你可以稍微修改代码:

class A {
    static int id = 0;
    public static void work(){
        id++;
    }
}

Then:

class B {
    public static void main(String[] args){
        System.out.println(A.id);
        A.work();
        System.out.println(A.id);
    }
}

This would produce:

这会产生:

0
1

Edit (with regard to your updated question)

编辑(关于您更新的问题)

Where are you specifying the update of the static int? From the code you've provided all you will do is print out the same int over and over as the method containing the increment process is never called.

你在哪里指定static int的更新?从你提供的代码中你要做的就是一遍又一遍地打印出相同的int,因为永远不会调用包含增量过程的方法。

Edit 2:

Try this:

Change:

handler.post(show);

to:

handler.postDelayed(show, 10000);