Access Modifiers:
- Access modifiers are used to set permissions to access the Class and its members (variables, methods & constructors).
- Java supports 4 access modifiers
- private
- <package> or <default>
- protected
- public
Note: We cannot apply access modifiers to blocks(we cannot access because no identity)
public class Pro { private int x; private Pro() { } protected void fun() { } static { // Error: } } |
Understanding the Access Modifiers with simple diagram:

Representing the above diagram in table form:
Access Modifier | Within the Class | Within the Package | Sub class of Same package | Sub class of other package | Other package |
private | Yes | No | No | No | No |
<default> | Yes | Yes | Yes | No | No |
protected | Yes | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes | Yes |