|
When you declare any class, you need to keep in mind that the objects that are created from same class share some commonality among them. For instance, all bank accounts have balance, interest rate etc. All banks accounts can also tell how much is the balance or who the account holder is. You can represent this commonality among all the bank accounts by declaring a class named Account. As you would expect, it will have member variables (for storing balance, interest raters etc) and methods (for getting balance etc). Listing 1.1 shows the Java class declaration for Account class.
![]() |
|
Note that, at minimum the class declaration must contain the class keyword and the name of the class followed by a pair of curly brackets {}. By convention, the class names start with a capital letter. It must be a legal Java identifier, which means that it must start with either a letter, $ or _ and can contain a letter, a digit, $ or _. For instance, you can declare a class named $9_a$$4. The body of Java class can have member declarations, which include the methods and the variables. As you can see, the Account class has two member variables, balance and accountHolder, for storing the state of the bank account objects. It also has two methods, getBalance() and withdraw() for implementing the common behavior among the bank account objects.