# Modifiers

<table data-full-width="true"><thead><tr><th width="127.33333333333337">Modifier</th><th width="329">Description</th><th>Example Code</th></tr></thead><tbody><tr><td>Public</td><td>Accessible from any class or package</td><td><code>public class ExampleClass { }</code></td></tr><tr><td>Private</td><td>Accessible only within the same class</td><td><code>private int exampleVariable;</code></td></tr><tr><td>Protected</td><td>Accessible within the same class and its subclasses</td><td><code>protected void exampleMethod() { }</code></td></tr><tr><td>Static</td><td>Belongs to the class rather than a specific instance</td><td><code>public static void exampleMethod() { }</code></td></tr><tr><td>Final</td><td>Cannot be modified once initialized</td><td><code>private final int EXAMPLE_CONSTANT = 10;</code></td></tr><tr><td>Abstract</td><td>Cannot be instantiated and must be extended</td><td><code>public abstract class ExampleAbstractClass { }</code></td></tr></tbody></table>

## Rules for Java Modifiers

Java modifiers allow controlling access to classes, variables, methods, etc. Here are some key rules:

### Access Modifiers

* `public` - Accessible from any class
* `protected` - Accessible within package and subclasses
* `default` (no modifier) - Accessible within the package only
* `private` - Accessible only within the class

For example:

```java
public class Main {

  private String name; //only accessible within Main
  
  void print() { //default access
    
  }

}
```

### Non-Access Modifiers

* `static` - Belongs to the class rather than an instance
* `final` - Cannot be inherited or modified
* `abstract` - Method signature without implementation
* `synchronized` - One thread access at a time

For example:

```java
public abstract class Parent {

  public static final int VALUE = 10;
  
  protected synchronized void print() {
    //...
  }

}
```

### Rules

* Only one public class per file
* Public class must match filename
* Interface methods are implicitly public
* Variables are default access if no modifier

So modifiers allow flexible access control in Java.

## **Java Access Levels**

* `private` - Access only within class 👪
* `default` - Access within package 📦
* `protected` - Access within package + subclasses outside package 👪👫
* `public` - Access everywhere 🌎

## **Access Summary**

<table data-full-width="true"><thead><tr><th>Modifier</th><th>Class</th><th>Package</th><th>Subclass</th><th>Outside</th></tr></thead><tbody><tr><td>private</td><td>Y</td><td>N</td><td>N</td><td>N</td></tr><tr><td>default</td><td>Y</td><td>Y</td><td>N</td><td>N</td></tr><tr><td>protected</td><td>Y</td><td>Y</td><td>Y</td><td>N</td></tr><tr><td>public</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td></tr></tbody></table>

## **Key Points:**

* `private` is most restrictive, `public` is most open
* Omitting access modifier defaults to package-level access
* `protected` gives subclass access outside package
* Understanding access modifiers is key to encapsulation
