class - JAVA FX: Can I access objects instantiated in main from any controller? -
the reason asking find out if can hold authentication details in class instantiated in main , reference them in various controllers?
public class identity(){ public string userid = null; } public class main extends application(){ identity identity = new identity; identity.userid = 123; //can access controller now? //i think when instantiate object in new controller //userid again null reference correct? }
u can use singlton design pattern.
class app { private static app app; private identity identity; private app() { } public static app app() { if (app == null) app = new app(); return app; } public void setidentity(identity identity) { this.identity = identity; } public identity getidentity() { return identity; } class identity { public string userid = null; public void setuserid(string userid) { this.userid = userid; } public string getuserid() { return userid; } } } import static app.*; public class yourcontroller { app().getidentity(); } import static app.*; public class main extends application(){ identity identity = new identity; identity.userid = 123; app().setidentiry(identity); }
Comments
Post a Comment