0

I have a function in whitch I use a global variable, and in this function I update this variable. I want to not update the value of this varible outside of this function.

public static int a = 0; //is global 
public static int b = 0;

 public void func(){
  if(a>c){
    a=a+b;
     }
 }

This function is called at 100ms

         final Handler handler = new Handler();
          Runnable runnable = new Runnable() {
            @Override
            public void run() {
            func();
               handler.postDelayed(this, 100);
           }
            };
            handler.postDelayed(runnable, 100);

Why "a" is initalized with zero everytime?

I want to do something like this:

 public void func(){
  int a=0;
  int b=0;
  while(a>c){
    a=a+b;
     }
 }

But I cannot to use while, because "c" is computed in another function, and if I use while, the value of "c" is not changed...

How I can solve this problem?

1 Answers1

0

Make function static. Static variables are changed by static functions.

Virgil Ionescu
  • 327
  • 1
  • 4
  • 9