public class Bank{
private CheckingAccount accountOne;
private CheckingAccount accountTwo;
public Bank(){
accountOne = new CheckingAccount("Zeus", 100, "1");
accountTwo = new CheckingAccount("Hades", 200, "2");
}
public static void main(String[] args){
Bank bankOfGods = new Bank();
System.out.println(bankOfGods.accountOne.name);
bankOfGods.accountOne.addFunds(5);
bankOfGods.accountOne.getInfo();
}
}
I dont understand why I don´t need to declare
private CheckingAccount accountOne;
private CheckingAccount accountTwo;
Why I don´t need to declare it as a String and what type of data is CheckingAccount accountOne? Is CheckingAccount the name of the variable or accountOne?
Should it not be something like that?
String private CheckingAccount ;
or
String private accountone;
The operator new
is the clue. It allocates memory on the heap for the given object (in this case an object of class CheckingAccount
).
The type of CheckingAccount
is in fact CheckingAccount
. Somewhere in your setup you have access to this CheckingAccount
’s API defined. In order to see exactly how CheckingAccount
is implemented you’d have to see it’s implementation file, but use it you just have to know its API (that is, the public methods available to whoever is using it).
It could very well just be a string, but more likely it’s a collection of properties (imagine: CheckingAccount might contain a string id, float accountBalance, Customer owner, etc.)
2 Likes