Access modifiers are used for encapsulation: they allow you to organize your code in packages and classes and have only the βofficialβ public interface visible from the outside, while hiding implementation details (which you want to do so that you can change it later without telling anyone )
This is especially important (only?) When you release code as a library for other programmers. Even if the code is used only in your own program, it helps to structure larger programs into several packages.
It makes no sense to do everything personal if your program consists of only one class. However, this is a reasonable option: until you think that some other class should call it, make the method private.
The default modifier in Java (if nothing is specified) is protected by the package. This allows you to access the method or field from your own code (that is, code in the same package), but still hides it from anyone else. This is also a reasonable default option or a natural way to upgrade from a private one: before the code outside the package should call it, make the method a protected package.
More noticeable levels are protected (subclasses can see it) and public (everyone can see it). Before you do something public or secure, think about the interface, because it is difficult to change it later.
source share