First, through the interface to achieve
Second, static variable declaration
1 PackageTest.autorun;2 3 Importjava.util.LinkedList;4 ImportJava.util.Queue;5 6 Public classMyVar {7 Private Static intNcount=0;8 Private StaticQueue<string> queue=NewLinkedlist<string>();;9 Public intGetncount () {Ten returnncount; One } A Public voidSetncount (intncount) { -Myvar.ncount =ncount; - } the PublicQueue<string>Getqueue () { - returnqueue; - } - Public voidSetqueue (queue<string>queue) { +Myvar.queue =queue; - } + A}
Use in other classes
1MyVar myvar=NewMyVar ();2 3Myvar.getqueue (). Offer ("Hello");4Myvar.getqueue (). Offer ("world!");5 6 System.out.println (Myvar.getqueue (). Size ()); 7 String str; 8 while((Str=myvar.getqueue (). Poll ())! =NULL){ 9 System.out.print (str); Ten } One System.out.println (); ASystem.out.println (Myvar.getqueue (). Size ());
The ability to implement global variables in Java