In this article we will see access modifiers and non access modifiers in brief, we will see public, private, protected and default access level with example.

Access Modifiers in Java

In Java there are three access modifiers public, private and protected but there is one more access level called default. If any class, method or variable does not have public, private or protected access level than the access level is called default. So in Java everything has a access level, at least default.


Public

If a class, method, variable, interface or constructor is declared as public, it means we can access that anywhere in the project even from the classes of other packages. However if the public class we are trying to access is in a different package, and then the public class still need to be imported.


// declaring a interface public
public interface Test {
}
// declaring a class public
public class TestImpl {
}
	
// declaring a variable public
public int a;

//declaring a method public
public void printName(){
	
}

Protected

If a method, variable or constructor is declared as protected, it means we can access them in the same class it is declared in, any other class of the same package and subclasses in other packages. There is no meaning of making a class protected. Protected can not be applied to an interface, and not even to the variables and methods of an interface.


// declaring a variable protected 
protected int a;

//declaring a method protected
protected void printName(){
		
}



Private

If a Method, variable or constructor is declared as private, it means we can not access that outside the class it is declared in. This is the most restricted access scope. There is no meaning of making a class private.


// declaring a variable private
private int a;

//declaring a method private
private void printName(){
		
}

Private can not be applied to an interface, and not even to the variables and methods of an interface.


Default

If a method, class or variables in a class does not have any access modifier, it means that can be accessed from anywhere in the same package. Variables in an interface are implicitly public static final and methods in an interface are by default public and abstract, even if they seems to be having a default scope moreover private or protected can not be applied to them.


// declaring a interface in default scope
interface Test {
}
// declaring a class in default scope
class TestImpl {
}
	
// declaring a variable in default scope 
int a;

//declaring a method in default scope
void printName(){
	
}

Here one thing is important to note down; if a class A does not have access to class B than A can not access any variable or method of class B, no matters what the access level of B’s members is.



Non Access Modifiers in Java

Apart from access modifiers like public, protected and private, java has have few more non-access modifiers. There are two widely used and popular access modifiers. 1) Static 2) Final


1) Static keyword in java

In java static means association with class and not with its instances, all the instances share same copy of static members. A static variable is not associated with any instance of a class individually; a single copy of variable is being shared with all the instances of the class. We can call an static variables with class name using a dot(.) operator.

// declaring a variable static 
static int a;

//declaring a method static
static void printName(){
		
}

Like static variables, static methods can be used with class name; they are associated with class rather than instances of a class. We can not access non static members directly in a static method we need to call them with any instance of class they are declared in, but static members can be used directly in static methods. We can not use this and super in a static context. Only an inner class can be declared as static

public class Test {

// declaring a class static
	static class innerClass {
		
	}
}

A static block is executed at the time of class loading, even before the main function starts its execution.

// static block
static {
		
}

2) Final keyword in java

In java a final keyword can be assigned to any variable, method or a class. A final concrete variable can not change its value once initialized or assigned a value, a reference final variable can not point to some other object when start pointing to a object. A final method can not be overridden and a final class can not be inherited by any other class.

// declaring a class final, can not be extended
final class TestImpl {
}
// declaring a variable final, value can not be changed
final int a=1;

// declaring a reference variable final. imply can not point some other
// object
final TestImpl impl = new TestImpl();

// declaring a method final, can not be overriden
final void printName() {

}

In this article we look brief about access modifiers in Java. We will look final and static keywords in detail in upcoming articles. There are few more non-access modifiers in Java like abstract, synchronized and volatile. We will see about them in coming articles.

  • By Techburps.com
  • Feb 28, 2015
  • Core Java