1

The reason I am asking is to find out if I can hold authentication details in a class instantiated in Main and then 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 I access this from any controller now?
    //I think that when i instantiate the object in a new controller the 
    //userId will again be null for that reference correct?
}
silversunhunter
  • 1,112
  • 2
  • 8
  • 26
  • 1
    Take a look here: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml BTW: Assuming `userId` is not `static`, the answer is no and depending on the type of the field the field of the new instance contains `null` (type = `Integer`) or `0` (type = `int`)... – fabian Aug 18 '17 at 17:42
  • 1
    If you load the FXML from your `Main` class, then you can pass a reference from there to your controller(s). In that case, the controller(s) would have access to the object instantiated in `Main`. – James_D Aug 18 '17 at 17:48

2 Answers2

-1

You cannot I am afraid, if you instantiate the object within a new controller the userId will be null as your referencing that newly created object's userId and not the original.

your best bet is to persist the data using a DB or File and read the data to and from, that way the data can be accessed by any controller.

ArcherGilly
  • 149
  • 11
-1

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);

}
Amin Paydar
  • 77
  • 1
  • 8
  • Thanks Amin.. I have not used singleton design patterns yet so this is new to me.. I will research it. – silversunhunter Aug 19 '17 at 17:47
  • Note that in many (or most) cases the singleton pattern is considered a bad design. See https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons Also note that you cannot use this method if `App` is the `Application` subclass (with the `start()` method, as the launch process needs to be able to instantiate it via the default constructor. Instead, pass the object to the controller using the techniques in the [link @fabian provided in the comments below the question](https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml) – James_D Aug 20 '17 at 23:03
  • @James_D the App class isn't the javafx application class subclass . this is just an entity class – Amin Paydar Aug 21 '17 at 05:47