Home > Articles > Programming > Java

Like this article? We recommend

Clues from Access Modifiers

The access controls (public, protected, private, and package) give clues as to what the programmers wanted to share. The points of sharing are often the most important parts of a program, because they generally divide relevant functional units.

An executable program may have no public methods in it at all (except for main, and it's no longer required to be in a public class). For a small project, that's more or less okay, although it shows a programmer getting into some bad habits. The larger the project, the more important it is to divide the program into manageable chunks, making public only those chunks that need to be public (or protected, as the case may be).

One problem with that technique is when your coworkers come to you and say, "Hey, I need access to this method [or that class]." The easiest response is to make the method public, and give the modified version to your coworker (which may involve a lengthy trip through configuration management). But that's not necessarily the right response. Should the method be public? Are you exposing things you don't want to expose? Will this cause some disaster down the line?

To avoid having to answer requests for access from other team members, some programmers make everything public, which solves the immediate problem but deprives you, the maintenance programmer, of some valuable clues as to what the important methods are.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.