Use another class variable

0

I have a lot of problems when using getter and setter and without examples that help me, it's hard for me to understand what I want ..

I'll explain.

I have this part my code:

    // Acció button Aceptar
class BtnAceptar implements ActionListener {
    @SuppressWarnings("unused")
    @Override
    public void actionPerformed(ActionEvent e) {
        // Creem les variables que utilitzem per registrar usuaris etc..

        String soapEndpointUrl = "http://gamificacioc.com:80/ServerGIOC/webservices";
        String soapAction = "http://gamificacioc.com:80/ServerGIOC/comprovarLogin";

        //Recollim les dades dels buttons e inicialitzem variables
        String usuari = txtUsuari2.getText(); 
        String usuariContrasenya = textPassword2.getText();

       WebServiceCalls calls = new WebServiceCalls();
       Object authId = null;

        //Exemple sense recollir la info dels  butons ho tinc per probar directament sense recollir dels buttons
       // System.out.println(calls.comprovarLogin("fbarcia", "password"));

       //Recollin't la info dels buttons.
        SoapObject resposta = calls.comprovarLogin(usuari, usuariContrasenya);
         if (resposta == null)
         {
             JOptionPane.showMessageDialog(null, "Error falten dades");
         }

         else
         {
             //Si els buttons son plens verifica que sigui correcte
            authId= resposta.getPrimitiveProperty("authId");
            //authId ens dona el resultat que volem pasar per saber el tipus User

         //Enviem l'authId per poder saber que usuari es...         
         String resposta2 = calls.tipusUsuari(authId.toString()); 
         System.out.println(authId);

The issue is that I need to recover the authId to use it in another class, but I would like it not to be with a global variable and I do not know how to do it ..

Can you help me understand?

thanks

    
asked by Montse Mkd 21.11.2018 в 15:04
source

1 answer

0

You should remove the authId property from the method and put it as private in the class and create a getter with your code more or less like that.

  // Acció button Aceptar
class BtnAceptar implements ActionListener {
    private Object authId = null;

    public Object getAuthId() {
       return this.authId;
    }

    @SuppressWarnings("unused")
    @Override
    public void actionPerformed(ActionEvent e) {
        // Creem les variables que utilitzem per registrar usuaris etc..

        String soapEndpointUrl = "http://gamificacioc.com:80/ServerGIOC/webservices";
        String soapAction = "http://gamificacioc.com:80/ServerGIOC/comprovarLogin";

        //Recollim les dades dels buttons e inicialitzem variables
        String usuari = txtUsuari2.getText(); 
        String usuariContrasenya = textPassword2.getText();

       WebServiceCalls calls = new WebServiceCalls();

        //Exemple sense recollir la info dels  butons ho tinc per probar directament sense recollir dels buttons
       // System.out.println(calls.comprovarLogin("fbarcia", "password"));

       //Recollin't la info dels buttons.
        SoapObject resposta = calls.comprovarLogin(usuari, usuariContrasenya);
         if (resposta == null)
         {
             JOptionPane.showMessageDialog(null, "Error falten dades");
         }

         else
         {
             //Si els buttons son plens verifica que sigui correcte
            this.authId= resposta.getPrimitiveProperty("authId");
            //authId ens dona el resultat que volem pasar per saber el tipus User

         //Enviem l'authId per poder saber que usuari es...         
         String resposta2 = calls.tipusUsuari(authId.toString()); 
         System.out.println(authId);

Being able to get the value of authId from the getter as follows btnAceptar.getAuthId()

    
answered by 21.11.2018 в 15:13