We will need to compile CreateAccount by means of the command
>
javac CreateAccount.java
Then we can run the program
>
java CreateAccount
A/c no: 123 A/e name: Fred Balance: 50.0
4.2 Class with Methods
As we have already mentioned, as well as member variables defining its state, a class can have
methods defining its behavior. A method is similar to a procedure or function in a nonobject-
oriented programming language. For our bank account class, examples of methods are deposit
and withdraw. The following is code for the deposit method that increases the balance by the
amount deposited:
public void
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.