I need to create a constructor that creates an account. It will ask the user to input an id number (4 digits) and it will create an account with $0 in it. Later the user will be able to add and subtract money, which I can do myself. I am just very confused about how to build a constructor. Here is my code:
import java.util.*;
public class Account{
public static void main(String[] args){
int id = 0;
double balance = 0;
Account account = new Account();
}
public Account(){
}
}
Is public Account() the constructor? or is it just an object? And if it IS the constructor, what exactly do I need to do create the account? (make the methods in the constructor or in another class?)
Yes
public Account(), is the constructor, constructors do not have a return type and they must be named exactly the same as the class.The constructor must be in the same class.
You do not need to do anything in your constructor, unless you want to initialize some of the attributes for your
Accountclass. The no-arg constructor instantiates an object, because the compiler will insert a call tosuper()in the first line of the constructor. Super will call the constructor up in the hierarchy tree all the way toObject.In fact you do not even have to write that no-arg constructor, unless you have another constructor in your Account class that takes one or more arguments, since the compiler will supply a no-arg constructor if there isn’t any explicit constructor.