When declaring a class, you can set whether the class can be accessed from any package in the application, or only from within the package in which it has been declared.
The general syntax specifying access-level for a class is
<access level modifier> class ClassName { // Body of the class goes here }
There are only two valid values for <access level modifier> in a class declaration: no value and public.
Access | Value | Level | Description |
---|---|---|---|
No value | Specify nothing. | package-level access. | class can be accessed only within the package in which it has been declared. |
public | public | from any package | can be accessed from any package in the application. |
The following code shows how to add public access to Employee.
public class Employee { String name; // Instance variable String gender; // Instance variable static long count; // Class variable }
The following code shows how to set package level access to Employee.
class Employee { String name; // Instance variable String gender; // Instance variable static long count; // Class variable }