Constructors

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

BankAccount.java
   /**
    * Create a new bank account with a balance of 0
    */
   public BankAccount() {
      balance = 0;
   }

*Constructors look like other methods but they are named after the class and have no return type.
 
*Constructors are invoked by the new operator after the storage for the newly created instance has been allocated but before anything else happens with the new object.
 
*While all instance variables are initialized by some default values, constructors allow to chose other initial values, or to make the choice of the default value less accidental.
 
*Constructors that take no parameters are called default constructors. If no constructor at all was specified for a class, an empty default constructor is implicitly defined by the Java programming language.
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 11, 2002