BankAccount a = new BankAccount( ); // Object a a.accNum = 123; a.name = "John"; a.balance = 1000; BankAccount b; // The copy /* ================================== How to copy an object in Java ================================== */ b = new BankAccount( ); // Create a new object b.accNum = a.accNum; // Copy old values over b.name = a.name; // to the new object b.balance = a.balance; |
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 using value given in parameters ==================================================== */ public BankAccount(int a, String n, double amount) { accNum = a; name = n; balance = amount; } ..... (other methods are omitted because they are not relevant) ..... } |
Reminder:
|
public class Class12 { public static void main(String[] args) { BankAccount a = new BankAccount( 123, "John", 1000 ); // This statement will invoke the constructor 1: // BankAccount(int a, String n, double amount) BankAccount b = new BankAccount( a );' // There is no constructor with this format // defined in the BankAccount class } } |
public class BankAccount { private int accNum; // Private access private String name; // -- limit to ONLY inside private double balance; // the class "BankAccount" /* ==================================================== Constructor 2: initialize all 3 instance variables using value given object x ==================================================== */ public BankAccount( BankAccount x ) { accNum = 'value of accNum in object x'; name = 'value of name in object x'; balance = 'value of amount in object x'; } ..... (other methods are omitted because they are not relevant) ..... } |
The 'value of accNum in object x' is
expressed in Java as:
x.accNum.
The 'value of name in object x' is
expressed in Java as:
x.name.
The 'value of balance in object x' is
expressed in Java as:
x.balance.
Therefore, the constructor method (Constructor 2 below) is written as follows (I have including the other method for completeness):
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 using values given in parameter variables ========================================================== */ public BankAccount(int a, String n, double amount) { accNum = a; name = n; balance = amount; } /* ==================================================== Constructor 2: initialize all 3 instance variables using value given object x ==================================================== */ public BankAccount( BankAccount x ) { accNum = x.accNum; name = x.name; balance = x.balance; } /* ============================================== 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 } } |
public class CopyObject3 { public static void main(String[] args) { BankAccount a = new BankAccount( 123, "John", 1000 ); BankAccount b = new BankAccount( a ); // **** make object b using object a /* ==================================== Let's do the experiment again.... ==================================== */ /* ---------------------------------- Print the values before the update ---------------------------------- */ System.out.println("Object a: " + a.convToString()); System.out.println("Object b: " + b.convToString()); b.deposit(444); // **** Update the object b /* ---------------------------------- Print the values after the update (Check if original changes) ---------------------------------- */ System.out.println("Values after updating the copy b:"); System.out.println("Object a: " + a.convToString()); System.out.println("Object b: " + b.convToString()); } } |
Output:
Object a: Account number: 123, Name: John, Balance: 1000.0 Object b: Account number: 123, Name: John, Balance: 1000.0 Values after updating the copy b: Object a: Account number: 123, Name: John, Balance: 1000.0 Object b: Account number: 123, Name: John, Balance: 1444.0 |
Updating the copy does not update the original
It is a true copy !
How to run the program:
|
|
/* ==================================================== Constructor 2: initialize all 3 instance variables using value given object x ==================================================== */ public BankAccount( BankAccount x ) { accNum = x.accNum; name = x.name; balance = x.balance; } |
is the copy constructor for the class BankAccount.