Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

EdwardKingNov 9 2012 — edited Nov 12 2012
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 10 2012
Added on Nov 9 2012
5 comments
210 views