Example & Tutorial understanding programming in easy ways.

What are access modifiers?

Modifiers are keywords that you add to those definitions to change their meanings. The Java language has a wide variety of modifiers, including the following:

1)Java Access Modifiers

2)Non Access Modifiers

Java Access Modifiers:

 For better understanding, member level access is formulated as a table: 

Access Modifiers
 
Same Class Same Package Subclass Other packages
public Y Y Y Y
protected Y Y Y N
no access modifier Y Y N N
private Y N N N

Visible to the package. the default. No modifiers are needed.

Visible to the class only (private).

Visible to the world (public).

Visible to the package and all subclasses (protected).

Variables and methods can be declared without any modifiers, as in the following examples:

String version = "4.5.6";

boolean processOrder() {
   return true;
}
To use a modifier, you include its keyword in the definition of a class, method, or variable. The modifier precedes the rest of the statement, as in the following 

example:

public class className {

// ...}

private boolean myFlag;

static final double weeks = 9.5;

protected static final int BOXWIDTH = 42;

public static void main(String[] arguments) {


Non Access Modifiers:

1)Java provides a number of non-access modifiers to achieve many other functionality.

2)The static modifier for creating class methods and variables

3)The final modifier for finalizing the implementations of classes, methods, and variables.

4)The abstract modifier for creating abstract classes and methods.
     

5)The synchronized and volatile modifiers, which are used for threads.
 

Read More →