Java Reference
In-Depth Information
balance = ac.getBalance();
System.out.println("Balance = " + balance);
// Attempt to debit more than the balance
ac.debit(2000.00);
balance = ac.getBalance();
System.out.println("Balance = " + balance);
}
}
Balance = 0.0
Crediting amount: 234.78
Debiting amount: 100.12
Balance = 134.66
Invalid credit amount: -234.9
Invalid debit amount: Infinity
Balance = 134.66
Insufficient fund. Debit attempted: 2000.0
Balance = 134.66
One important point to keep in mind when you design a class is its maintainability. Keeping all instance variables
private
and allowing access to them through
public
methods makes your code ready for future changes. Suppose
you started with a zero minimum balance for an account. You have deployed the
Account
class in the production
environment and it is being used in many places in the application. Now, you want to implement a new business rule
that states that every account must have a minimum balance of 100. It is easy to make this change. Just change the
code for the
debit()
method and you are done. You do not need to make any changes to the client code that is calling
the
debit()
method of the
Account
class. Note that you need a little more work on the
Account
class to fully enforce
the rule of a minimum balance of 100. When an account is created, the balance is zero by default. To enforce this new
minimum balance rule at the time an account is created, you will need to know about constructors of a class. I will
discuss constructors later in this chapter.
Another option for the access level for the
balance
instance variable in the
Account
class is to give it a package-
level access. Recall that a package-level access is given to a class member by using no access modifier in its
declaration. If the
balance
instance variable has package-level access, it is a little better than giving it
public
access
because it is not accessible from everywhere. However, it can be accessed and manipulated directly by the code inside
the same package in which the
Account
class has been declared. We all understand that letting any code access the
balance
instance variable directly from outside the
Account
class is not acceptable. Additionally, if you declare the
method of the
Account
class to have package-level access, it can be used only inside the same package in which the
Account
class has been declared. You want objects of the
Account
class to be manipulated from anywhere in the
application using its methods. Therefore, you cannot declare the methods or the instance variable of the
Account
class to have package-level access. When do you declare a class and/or a class member to have package-level access?
Typically, package-level access is used for a class and its members when the class has to serve as a helper class or
internal implementation for other classes in a package.
When do you use a
private
access level for class members? You have already seen the benefits of using the
private
instance variables for the
Account
class. The
private
access level for instance variables provides data
hiding, where the internal state of the object is protected from outside access. An instance method for a class defines
a behavior for its objects. If a method is used only internally within a class, and no outside code has any business
knowing about it, the method should have a
private
access level. Let's go back to your
Account
class. You have used
the same logic to validate the amount passed to the
credit()
and
debit()
methods. You can move the code that