i creating book , i'm interested in globalizing variables (objects).
in 1 project can singletone: if use in library , give value, if in project consult value 0.
library:
public class globals{ private static globals instance; // global variable private int data; // restrict constructor being instantiated private globals(){} public void setdata(int d){ this.data=d; } public int getdata(){ return this.data; } public static synchronized globals getinstance(){ if(instance==null){ instance=new globals(); } return instance; } /*then write methods here*/ public void yourmethod(){} }
library: class examplelib
globals g = globals.getinstance(); g.setdata(120));
myproject
globals g = globals.getinstance(); system.out.println(g.getdata()); // show 0
Comments
Post a Comment