Search code examples
javaswinguser-interfacenetbeans

how can i reference the instance of an object?


Here's the thing i create a new instance of a class in a file then i'm trying to reference that specific instance in another file.

This is the initilization of the object in RegistroUI.

CuentaBancaria Cuenta = new CuentaBancaria(nombre);

This is my code trying to reference that same class in IngresoUI.

textBienvenido.setText("BIENVENID@ USUARIO" + Cuenta.getNombre());

All of this is in the same package and written in java. Please any idea will help :D

I can't create another object because it won't have the same values for attributes and i don't need to import because is already in the same package.


Solution

  • To reference an instance of an object, you need to pass it somehow into IngresoUI. For example via injection in constructor of IngresoUI.

    CuentaBancaria cuenta;
    IngresoUI(CuentaBancaria cuenta){
        this.cuenta=cuenta
    }
    

    Another method is to inject IngresoUI into RegistroUI and then pass CuentaBancaria cuenta into some method of IngresoUI.

    IngresoUI ingresoUI;
    RegistroUI( IngresoUI ingresoUI){
        this.ingresoUI = ingresoUI;
    }
    void someMethodOfRegistroUI(){ //Method where you create CuentaBancaria 
        int nobre = 10;
        CuentaBancaria cuenta = new CuentaBancaria(nombre);
        ingresoUI.someMethodOfIngresoUI(cuenta);//call a method of IngresoUI
        //where you need CuentaBancaria cuenta;
    }