I didn't know very well how to title the question, and for the same reason I don't know very well how to put it in a simpler way, but here I go...
I have a bank management project. In it, I have the Principal (where the main is), Person (where I create account holders and authorized persons), BankAccount and Bank classes .
In the exercise I am asked to create a Bank class with a void StartData() method that is in charge of creating a few bank accounts to be able to "play" with the application.
My problem is that I don't know how to call the method from Main to create the initial Bank object.
public class Banco {
private String nombre;
private HashMap<String, CuentaBancaria> cuentas = new HashMap<>();
public Banco(String nombreBanco, HashMap cuentas){
this.nombre = nombreBanco;
this.cuentas = cuentas;
}
public void datosInicio(){
Persona t1 = new Persona("11223344A", "Gracia Macías");
Persona t2 = new Persona("22334455B", "Armando Líos");
Persona t3 = new Persona("33445566C", "Dolores Fuertes");
Persona t4 = new Persona("44556677D", "Lindo Gatito");
Persona t5 = new Persona("55667788E", "Gloria Bendita");
CuentaBancaria c1 = new CuentaBancaria(1001, t1);
CuentaBancaria c2 = new CuentaBancaria(1002, t2);
CuentaBancaria c3 = new CuentaBancaria(1003, t3);
CuentaBancaria c4 = new CuentaBancaria(1004, t4);
CuentaBancaria c5 = new CuentaBancaria(1005, t5);
c1.ingresar(250);
c2.ingresar(500);
c3.ingresar(750);
c4.ingresar(1000);
c5.ingresar(1250);
cuentas.put("1001", c1);
cuentas.put("1002", c2);
cuentas.put("1003", c3);
cuentas.put("1004", c4);
cuentas.put("1005", c5);
Banco miBanco = new Banco("Banca Ética DAM", cuentas);
}
}
In the Main class , and this is where I'm not clear, I try to create the Bank object I need inside the main() method
public static void main(String[] args) {
Banco miBanco;
miBanco.datosInicio();
}
I would appreciate any kind of help, including changing the title of my question so that it can help the next person who comes up with a similar problem.
PS The error I get is...
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable miBanco might not have been initialized
at a_basico.Principal.main(Principal.java:23)
C:\Users\jakob\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\jakob\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 1 second)
And line 23 you are talking about is
miBanco.datosInicio();
You are initializing again
Banco
when callingdatosInicio()
which is not correct, I suggest you do the following, when calling your methoddatosInicio()
to initialize the data, assign the accounts to the object:Now to correctly initialize your object
Banco
, create aHashMap
in which the account data will be filled and initialize your object, then calldatosInicio()
and your object will have all the account data:Note that the object
miBanco
is being declared but never created as such. This must be created with the syntaxIf you do not send parameters to the constructor, in the Bank class there must be a constructor that does not receive parameters and that initializes its own objects or variables. So:
Already after creating the object, if you should be able to access the method of
You can change the constructor of Bank, and remove the argument
cuentas
to be like this:Then, in the StartData() method you remove the line
Banco miBanco = new Banco("Banca Ética DAM", cuentas);
:Finally in the main you would only have to create the Bank object and call the method.
As a side note, you can create the getAccounts() method to get the Bank accounts.
The contradiction is that you create the instance of the object from
datosInicio()
, but being of the typevoid
then you can't return that instance, it gets stuck in there.You could create a constructor in the class
Banco
that only admits the name, for cases in which only that data is known when creating the object, as in this exercise, because the other elements are created from within the same class. This is called multiple constructors and is common practice in Java.Then from
main
it you create the object using that constructor and passing it the name of the bank.If the requirement is different, since it is an exercise, you can leave it as you have it, but you must create an empty constructor, create the instance from the
main
withnew Banco()
anddatosInicio()
also set the name as you do with the accounts:this.nombre="Banca Ética DAM";
Here we instantiate the object by passing it the name of the bank:
This is how it should work.
As one of the answers told you, the only thing you are executing in the main function of your program is the declaration of the variable, without its corresponding assignment (which I understand is your intention and problem).
In Java, the way to return a new instance of a class is by using the syntax:
Substituting "Class" for the name of the class that you want to instantiate, and params being one or more variables (totally optional) of the constructor of said class so that you would have something like this:
I suspect that you are following a course that requires prior knowledge of Object Oriented Programming .
Taking a closer look at your case, I see that you want to create a single instance of Bank, furthermore, from your attempt to create said instance from the same class, it makes me think that you are basing it on a software design pattern called Singleton
Where the solution to your problem would be to remove the parameters from your constructor and add the following code to your Bank class:
So in your main class you can just do this:
Either solution is valid!