Skip to main content
Lesson 6 - Defining and Using Classes
Lesson MenuPreviousNext
  
Constructors page 7 of 11

  1. The final requirement to implement the CheckingAccount class is to define a constructor, whose purpose is to initialize the values of instance variables of an object.

    public class CheckingAccount
    {
      ...
      public CheckingAccount()  // constructor
      {
        myBalance = 0;
        myAccountNumber = "NEW";
      }
      ...
    }
  2. Constructors always have the same name as their class. Similar to methods, constructors are generally declared as public to enable any code in a program to construct new objects of the class. Unlike methods, constructors do not have return types.

  3. Constructors are always invoked together with the new operator:

    new CheckingAccount();

    The new operator allocates memory for the objects, and the constructor initializes it. The value of the new operator is the reference to the newly allocated and constructed object.

    In most cases, you want to declare and store a reference to an object in an object variable as follows:

    CheckingAccount checking = new CheckingAccount();
  4. If you do not initialize an instance variable that is a number, it is initialized automatically to zero. Even though, initialization is handled automatically for instance variables, it's a matter of good style to initialize all instance variables explicitly.

  5. Many classes have more than one constructor. For example, you can supply a second constructor for the CheckingAccount class that sets the mybalance and accountNumber instance variables to initial values, which are the parameters of the constructor:

    public class CheckingAccount
    {
      ...
    
      public CheckingAccount() // constructor defines values
      {
        myBalance = 0;
        myAccountNumber = "NEW";
      }
    
      public CheckingAccount(double initialBalance, String acctNum)
      // contructor gets values elsewhere
      {
        myBalance = initialBalance;
        myAccountNumber = acctNum;
      }
      ...
    }

    The second constructor is used if you supply a number and a string as construction parameters.

    CheckingAccount checking = new CheckingAccount(5000, "A123");
  6. Note that in the above example there are two constructors of the same name. Whenever you have multiple methods (or constructors) with the same name, the name is said to be overloaded. The compiler figures out which one to call by looking at the parameters of each method.

    For example, if you construct a new checkingAccount object with

    CheckingAccount checking = new CheckingAccount();

    then the compiler picks the first constructor. If you construct an object with

    CheckingAccount checking = new CheckingAccount(5000, "A123");

    then the compiler picks the second constructor.

  7. The implementation of the checkingAccount class is complete and is given below:

    public class CheckingAccount
    {
      private double myBalance;
      private String myAccountNumber;
    
      public CheckingAccount()
      {
        myBalance = 0;
        myAccountNumber = "NEW";
      }
    
      public CheckingAccount(double initialBalance, String acctNum)
      {
        myBalance = initialBalance;
        myAccountNumber = acctNum;
       }
    
      public double getBalance()
      {
        return myBalance;
      }
    
      public void deposit(double amount )
      {
        myBalance =  myBalance + amount;
      }
    
      public void withdraw(double amount )
      {
        myBalance =  myBalance - amount;
      }
    }


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.