Example:
![]() |
|
|
Purpose of constructor methods:
|
|
Example:
public class BankAccount { private int accNum; // Instance variables private String name; // private double balance; // public BankAccount( .... ) // This is a constructor method { ... used to initialize the instance variables } public void deposit( .... ) // This is NOT a constructor method { ... } } |
|
|
public class BankAccount { private int accNum; // Private access private String name; // -- limit to ONLY inside private double balance; // the class "BankAccount" /* ==================================================== Constructor 1: initialize all 3 instance variables ==================================================== */ public BankAccount(int a, String n, double amount) { System.out.println("Test message: invoking Constructor 1"); // For demo only ! accNum = a; name = n; balance = amount; } /* ==================================================== Constructor 2: initialize all 2 instance variables (opens an account with balance = 0) ==================================================== */ public BankAccount(int a, String n) { System.out.println("Test message: invoking Constructor 2"); // For demo only ! accNum = a; name = n; balance = 0.0; } /* ============================================== convToString(): return a String containing information of BankAccount ============================================== */ public String convToString( ) { return( "Account number: " + this.accNum + ", Name: " + this.name + ", Balance: " + this.balance); } /* ============================================== deposit(amount): Add "amount" to balance ============================================== */ public void deposit( double amount ) { this.balance += amount; } /* ====================================================== withdraw(amount): Subtract "amount" from balance ====================================================== */ public void withdraw( double amount ) { if ( this.balance >= amount ) this.balance -= amount; // Subtract "amount" from balance } } |
|
There can be many different constructors...
Which one will be used ???
|
|
public class Class07 { public static void main(String[] args) { String x; BankAccount stu1 = new BankAccount(123, "John", 1000.0); // Use constructor 1 x = stu1.convToString( ); System.out.println("stu1: " + x); BankAccount stu2 = new BankAccount(456, "Mary"); // Use constructor 2 x = stu2.convToString( ); System.out.println("stu2: " + x); BankAccount stu3 = new BankAccount(123, "James", 1000); // Use constructor 1 x = stu3.convToString( ); System.out.println("stu1: " + x); } } |
Output:
Test message: invoking Constructor 1 stu1: Account number: 123, Name: John, Balance: 1000.0 Test message: invoking Constructor 2 stu2: Account number: 456, Name: Mary, Balance: 0.0 Test message: invoking Constructor 1 stu3: Account number: 123, Name: James, Balance: 1000.0 |
Note:
|
How to run the program:
|
|
|
public class BankAccount { private int accNum; // Private access private String name; // -- limit to ONLY inside private double balance; // the class "BankAccount" // Does NOT contain any constructor method ! /* ============================================== convToString(): return a String containing information of BankAccount ============================================== */ public String convToString( ) { return( "Account number: " + accNum + ", Name: " + name + ", Balance: " + balance); } /* ============================================== deposit(amount): Add "amount" to balance ============================================== */ public void deposit( double amount ) { balance += amount; } /* ====================================================== withdraw(amount): Subtract "amount" from balance ====================================================== */ public void withdraw( double amount ) { if ( balance >= amount ) balance -= amount; // Subtract "amount" from balance } } |
public class BankAccount
{
private int accNum; // Private access
private String name; // -- limit to ONLY inside
private double balance; // the class "BankAccount"
// Automatically inserted !
public BankAccount()
{
// No statements !
}
/* ==============================================
convToString(): return a String containing
information of BankAccount
============================================== */
public String convToString( )
{
return( "Account number: " + accNum
+ ", Name: " + name
+ ", Balance: " + balance);
}
/* ==============================================
deposit(amount): Add "amount" to balance
============================================== */
public void deposit( double amount )
{
balance += amount;
}
/* ======================================================
withdraw(amount): Subtract "amount" from balance
====================================================== */
public void withdraw( double amount )
{
if ( balance >= amount )
balance -= amount; // Subtract "amount" from balance
}
}
|
new BankAccount() |
We saw this before in this example: (See: click here)
public class Class06 { public static void main(String[] args) { BankAccount stu1; stu1 = new BankAccount(); // Create one BankAccount object with // 3 data fields: // stu1.studID, stu1.name, stu1.major stu1.accNum = 12345; stu1.name = "Mary"; stu1.balance = 1000.0; String x; x = stu1.convToString( ); System.out.println("stu1: " + x); stu1.deposit( 450 ); x = stu1.convToString( ); System.out.println("After depositing $450: " + x); stu1.withdraw( 100 ); x = stu1.convToString( ); System.out.println("After withdraw $100: " + x); } } |
new BankAccount( 123, "John", 1000.0) |
because there is no constructor with matching parameters
public class Class08 { public static void main(String[] args) { String x; BankAccount stu1 = new BankAccount(123, "John", 1000.0); // Error BankAccount stu2 = new BankAccount(456, "Mary"); // Error } } |
Compiler output:
Class08.java:7: cannot find symbol symbol : constructor BankAccount(int,java.lang.String,double) location: class BankAccount BankAccount stu1 = new BankAccount(123, "John", 1000.0); ^ Class08.java:10: cannot find symbol symbol : constructor BankAccount(int,java.lang.String) location: class BankAccount BankAccount stu2 = new BankAccount(456, "Mary"); ^ 2 errors |
How to run the program:
|
public class BankAccount { private int accNum; // Private access private String name; // -- limit to ONLY inside private double balance; // the class "BankAccount" /* ==================================================== Constructor 1: initialize all 3 instance variables ==================================================== */ public BankAccount(int a, String n, double amount) { System.out.println("Test message: invoking Constructor 1"); // For demo only ! accNum = a; name = n; balance = amount; } /* ==================================================== Constructor 2: initialize all 2 instance variables (opens an account with balance = 0) ==================================================== */ public BankAccount(int a, String n) { System.out.println("Test message: invoking Constructor 2"); // For demo only ! accNum = a; name = n; balance = 0.0; } /* ============================================== convToString(): return a String containing information of BankAccount ============================================== */ public String convToString( ) { return( "Account number: " + accNum + ", Name: " + name + ", Balance: " + balance); } /* ============================================== deposit(amount): Add "amount" to balance ============================================== */ public void deposit( double amount ) { balance += amount; } /* ====================================================== withdraw(amount): Subtract "amount" from balance ====================================================== */ public void withdraw( double amount ) { if ( balance >= amount ) balance -= amount; // Subtract "amount" from balance } } |
The Java compiler will not include any more constructor method in the class.
new BankAccount(123, "John", 1000.0) and new BankAccount(456, "Mary") |
We saw this before in the above example: (See: click here)
public class Class07 { public static void main(String[] args) { String x; BankAccount stu1 = new BankAccount(123, "John", 1000.0); // Use constructor 1 x = stu1.convToString( ); System.out.println("stu1: " + x); BankAccount stu2 = new BankAccount(456, "Mary"); // Use constructor 2 x = stu2.convToString( ); System.out.println("stu2: " + x); BankAccount stu3 = new BankAccount(123, "James", 1000); // Use constructor 1 x = stu3.convToString( ); System.out.println("stu1: " + x); } } |
new BankAccount( ) |
because there is no constructor with matching parameters !!!!!
public class Class09 { public static void main(String[] args) { BankAccount stu1 = new BankAccount( ); // Error !!! } } |
Compiler output:
Class09.java:6: cannot find symbol symbol : constructor BankAccount() location: class BankAccount BankAccount stu1 = new BankAccount( ); ^ 1 error |
How to run the program:
|
|
|
|
public class BankAccount { private int accNum; // Private access private String name; // -- limit to ONLY inside private double balance; // the class "BankAccount" /* ==================================================== Constructor 1: initialize all 3 instance variables ==================================================== */ public BankAccount(int a, String n, double amount) { accNum = a; name = n; balance = amount; } /* ==================================================== Constructor 2: initialize all instance variables to some chosen default values (The default constructor) ==================================================== */ public BankAccount( ) { accNum = -1; name = "Nobody"; balance = 0.0; } /* ==================================================== Constructor 3: initialize all instance variables to values in an existing object (The copy constructor) ==================================================== */ **** This method will be discussed in the next webpage **** .... other methods .... } |
|