This discussion is archived
5 Replies Latest reply: Nov 12, 2012 1:44 AM by Kayaman RSS

How to make class only initial once and be called to share a variable?

282391 Newbie
Currently Being Moderated
I have a class,like follows:
public class demo{
private int a1;
private boolean available=false;
public synchronized void set(int value){
a1=value
}
public synchronized int get(){
return a1;
}
}

I make another two class to call demo class,in aa class,I set a1=5 under demo,

public class aa{
public void setvalue{
demo d=new demo();
d.set(5);
}
}

And get value from like follows:

public class bb{
public void getvalue(){
demo d=new demo();
System.out.println(d.get());
}
}

My question,how to make demo class can be initial once and make invoked by many class,but share a variable?
Thanks

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points