BankAccountTest.java
1.// To run the program, use java BankAccountTest
2.// the output of the program is
3.// Charles has $18500 in his bank account.
4.public class BankAccountTest {
5. public static void main(String[] args) {
6. BankAccount account = new BankAccount("Charles", 20000);
7. account.deposit(500);
8. account.withdraw(2000);
9. System.out.println(account.getName() + " has $" + account.getSaving() +
10. " in his bank account.");
11. }
12.}
Constructor
Line 5-9 of BankAccount.java declare the constructor of
the class BankAccount
Constructor is neither memebers nor methods. We will discuss
this in more detail later.
The constructor show us how to initialize the value (
saving and name ).
Syntax
public classname(arugments) {
// your code
}
new
The way to create a new object is showed on line 6
BankAccount account = new BankAccount("Charles", 20000);
new BankAccount("Charles", 20000) return an instance (object)
of class BankAccount, with name is Charles and
saving is 20000.
Syntax
new classname(argument)
Method
Refer to line 7 , 8 and 9. To access the methods, we use the following
syntax
account.deposit(500);
| | |
| | |
object method arguments
name name
Syntax
objectname.methodname(arguments)
Summary
After this section, you should know how to
- create an object by
new operator.
- use methods.