Website For Developers

Access modifiers in Java

Default access modifier

Example of default access modifier

// variable with default modifier
String = "Equality and Justice are not the same thing.";
// method with default modifier
void display () {

private access modifier

Example of private access modifier

// variable with private modifier
private String = "All people should have equal rights.";
// method with private modifier
private void display () {

public access modifier

Example of public access modifier

// variable with public modifier
public String = "Fight for your rights till your last breath.";
// method with public modifier
public void display () {

Note: The main() method of the program must be public.
public static void main (String [] args) {

protected access modifier

Example of protected access modifier

// variable with protected modifier
protected String = "Worst form of equality is when people try to equal the unequal things.";
// method with protected modifier
protected void display () {