Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)

share|improve this question
function work()? – Sajal Dutta Aug 19 at 20:24
this is not even valid java syntax – Kon Aug 19 at 20:24
What is function work()? That's not even valid input?! – Andrew Martin Aug 19 at 20:25
it is just an example.. i mean that i am updating it's value in a function – dracula Aug 19 at 20:26
1  
Well write out your exact code that you are using - after all, it's a fairly short amount of code – Andrew Martin Aug 19 at 20:26
show 5 more comments

4 Answers

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());
        }

    }
}
share|improve this answer
this doesn't updates value of id in class A – dracula Aug 19 at 20:32
@dracula: Yes it does. Are you sure you've copied that code over exactly? – Andrew Martin Aug 19 at 20:36
updated my question , can you give a look – dracula Aug 19 at 20:44
add volatile to public static int postID = 1; – Bouchehboun Saad yesterday

You need to call work to execute 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:

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.

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

javac B.java

After compilation, to execute/run-

java B

share|improve this answer
@dracula See my updated answer. – Sajal Dutta Aug 19 at 20:42

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:

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.

Edit 2:

Try this:

Change:

handler.post(show);

to:

handler.postDelayed(show, 10000);
share|improve this answer
As i wrote in comments , i am calling updatePostID() with a handler that run each 10 seconds. – dracula Aug 19 at 20:57
@dracula: And where is the code for that? – Andrew Martin Aug 19 at 20:58
it is in a runnable,look code. – dracula Aug 19 at 21:07
@dracula: See my edit – Andrew Martin Aug 19 at 21:14
There is not any problem with handler. You understand my problem. If i access postID in Class A it gives me incremented value , if call it from Class B it gives 1 , but it is 5 normally (just an example) – dracula Aug 19 at 21:22

class A { static int id=0;

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

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);
}

}

share|improve this answer
I think you need to review your code. This does not do what you intend it to do - why are you creating two objects of class A, then referencing it statically (and the static block is horrible! Put it in a method). – Andrew Martin Aug 19 at 20:38
Just a counter example, it will increase value each time you create a new instance – Nick Aug 19 at 20:42
You haven't explained that though. Additionally, it's very counter-intuitive to increase the int by creating new objects of the class when the objects aren't used and the int is accessed statically. – Andrew Martin Aug 19 at 20:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.