Access Control

Java has four levels of controlling access to fields, methods, and classes:

Can be accessed by methods of all classes

Can be accessed only by the methods of their own class

Protected features can be accessed by all subclasses and all classes in the same package.

Recommended Access Levels


What is a common reason for defining package-visible instance fields?

   Answer: Accidentally forgetting the private modifier.


If a class with a public constructor has package access, who can construct objects of it?

   Answer: Any methods of classes in the same package.