Java Access Modifiers
When creating our classes and defining the properties and methods in our class, we want to implement some kind of restriction to access these data. For example, if you want a certain attribute to be changed only by the methods inside the class, you may want to hide this from other objects using your class. In Java, we have what we call access modifiers in order to implement this.
There are four different types of member access modifiers in Java: public, private, protected and default. The first three access modifiers are explicitly written in the code to indicate the access type, for the fourth one which is default, no keyword is used.
default access (also called package accessibility)
This specifies that only classes in the same package can have access to the class' variables and methods. There are no actual keyword for the default modifier; it is applied in the absence of an access modifier. For example,
public class StudentRecord
{
//default access to instance variable
int name;
//default access to method
String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed from other objects, as long as the object belongs to the same package where the class StudentRecord belongs to.
public access
This specifies that class members are accessible to anyone, both inside and outside the class. Any object that interacts with the class can have access to the public members of the class. For example,
public class StudentRecord
{
//default access to instance variable
public int name;
//default access to method
public String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed from other objects.
protected access
This specifies that the class members are accessible only to methods in that class and the subclasses of the class. For example,
public class StudentRecord
{
//default access to instance variable
protected int name;
//default access to method
protected String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed
only from methods inside the class and from subclasses of StudentRecord. We will discuss about subclasses on the next chapter.
private access
This specifies that the class members are only accessible by the class they are defined in. For example,
public class StudentRecord
{
//default access to instance variable
private int name;
//default access to method
private String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed
only from methods inside the class.
Coding Guidelines:
The instance variables of a class should normally be declared private, and the class will just provide accessor and mutator methods to these variables.
When creating our classes and defining the properties and methods in our class, we want to implement some kind of restriction to access these data. For example, if you want a certain attribute to be changed only by the methods inside the class, you may want to hide this from other objects using your class. In Java, we have what we call access modifiers in order to implement this.
There are four different types of member access modifiers in Java: public, private, protected and default. The first three access modifiers are explicitly written in the code to indicate the access type, for the fourth one which is default, no keyword is used.
default access (also called package accessibility)
This specifies that only classes in the same package can have access to the class' variables and methods. There are no actual keyword for the default modifier; it is applied in the absence of an access modifier. For example,
public class StudentRecord
{
//default access to instance variable
int name;
//default access to method
String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed from other objects, as long as the object belongs to the same package where the class StudentRecord belongs to.
public access
This specifies that class members are accessible to anyone, both inside and outside the class. Any object that interacts with the class can have access to the public members of the class. For example,
public class StudentRecord
{
//default access to instance variable
public int name;
//default access to method
public String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed from other objects.
protected access
This specifies that the class members are accessible only to methods in that class and the subclasses of the class. For example,
public class StudentRecord
{
//default access to instance variable
protected int name;
//default access to method
protected String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed
only from methods inside the class and from subclasses of StudentRecord. We will discuss about subclasses on the next chapter.
private access
This specifies that the class members are only accessible by the class they are defined in. For example,
public class StudentRecord
{
//default access to instance variable
private int name;
//default access to method
private String getName(){
return name;
}
}
In this example, the instance variable name and the method getName() can be accessed
only from methods inside the class.
Coding Guidelines:
The instance variables of a class should normally be declared private, and the class will just provide accessor and mutator methods to these variables.
Related Post:
Java Tutorial switch statement source codeJava tutorial source code get input from keyboard
Java Tutorial Using JOptionPane to get input
Java tutorial Decision Control Structures: if statement
PHP Tutorial
Java programing source code sample printing characters
Java tutorial if-else statement
C programming sample source code: Sum of 2 numbers
Java Programming Features



