Constructor Methods

  1. Constructor methods (or constructor for short) are methods to initialize instance variables in a class.

    ****   Phrased differently: Constructors are methods to initialize objects at the moment when they are created


  2. Constructors always have the same name as the class.

    Constructors do NOT have a return type (because they do not return anything)

    Constructors CAN have parameters

    Constructors are usually public

       class BankAccount
          public BankAccount()   <---- a constructor method for BankAccount
          public BankAccount(double value)   <---- another constructor method

  3. The constructors are always called together with the new operator

    The new operator obtains free (unused) memory space to store the instance variables

       new BankAccount()  
           1. "new" operator "creates a new object of type BankAccount"
              In layman's terms: obtain free/unused space to store
              the instance variables of the new object
           2. will invoke the constructor method "BankAccount()" 
    	  in class BankAccount (to initial the new instance variables)
       new BankAccount(2000.0)
           1. As before, the "new" operator creates a new object of 
    	  type BankAccount"
           2. it will now invoke the constructor  method 
    	  "BankAccount(double value)" in class BankAccount 
    	  (because Java uses Method Name PLUS parameters' type 
    	   to identify a method)

  4. You CANNOT invoke a constructor method using an object variable.

    Example: you CANNOT write the following...
     or   harrysChecking.BankAccount(1000.0);
    constructors can only be invoked at the moment that an new object is created !!!

  5. BankAccount class with 2 constructors:

    public class BankAccount { private double balance; // *********************************************** // Constructor 1: does not take any parameters // *********************************************** public BankAccount() { balance = 0.0; } // ********************************************************* // Constructor 2: take a double representing initial deposit // ********************************************************* public BankAccount(double initialBalance) { balance = initialBalance; } public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { if ( amount <= balance ) balance = balance - amount; else System.out.println("**** Insufficient funds to withdraw $" + amount); } public double getBalance() { return(balance); } }

  6. Here are a new set of programs that illustrate the constructor concept:


Default Constructor

  1. The constructor method without any parameters is called the default constructor.

  2. If the user does not provide a default constructor, then the Java compiler will automatically invoke the following empty method when an object is created without using any parameters (new ClassName()).

    Example: for the class BankAccount, if there is no default constructor, Java will execute: public BankAccount() { }

    The default constructor provided by Java does not initialize any variables...

    However... all instance variables start out with the value zero