Search⌘ K

Private Methods

Explore the concept of private methods in Java to understand how they protect class data and support modular programming. Learn why constructors use private methods to avoid risks associated with calling public methods and how this approach helps validate data fields safely. This lesson helps you design classes that maintain integrity by restricting client access to sensitive methods.

Methods that call other methods

We know that one method can call another. The invoked method can belong to a different class or to the same class as the method that calls it. For example, in the class Name, the method setName invokes setFirst and setLast. In this case, all three methods belong to the same class. The invoked methods are also public.

Sometimes the methods that a method calls are not suitable for use by the client of the class. Such methods usually perform only a part of computation and are called by a method to help it complete its task. We make these methods private instead of public.

Consider the class CalendarDate, discussed in the previous lesson. Both the ...