Modifiers In Java
There will be a number of Java modifiers covered in order to meet the needs of learners more efficiently.
Java Modifiers
When it comes to Java modifiers, you’re probably quite familiar with the public keyword that appears in most examples:
Java modifiers such as public are access modifiers that control access to classes, attributes, methods and constructors.
Modifiers can be divided into two categories:
- Access Modifiers – controls the access level
- Non-Access Modifiers – provides other functionality instead of controlling access level
Access Modifiers In Java
The Java class modifiers public and default can be used for classes:
Modifier | Overview | Try it |
---|
public | Access to the class is possible from any other class. | Try it |
default | Classes in the same package can only access the class. In the absence of a modifier, this will be used.
The Packages chapter contains more information about packages. | Try it |
In the case of attributes, methods, and constructors, choose one of the following:
Modifier | Overview | Try it |
---|
public | All classes have access to the code. | Try it |
private | A declared class is the only place where the code can be accessed. | Try it |
default | Access to the code is limited to the same package. In the absence of a modifier, this is used. | Try it |
protected | In the same package and subclasses, the code is accessible. The chapter on inheritance discusses subclasses and superclasses in more detail. | Try it |
Below is a brief table that explains Java’s access modifiers functionality.
Access Modifier’s | Within Class | Within Package | Outside Package (subclass only) | Outside Package |
---|
Private | Y | N | N | N |
Default | Y | Y | N | N |
Protected | Y | Y | Y | N |
Public | Y | Y | Y | Y |
Non-Access Modifiers
You can use either an abstract or a final modifier for classes:
Modifier | Overview | Try it |
---|
final | A class can’t be inherited by another class if it contains the final modifier. | Try it |
abstract | Objects cannot be created using the class with an abstract modifier (To access an abstract class, it must be inherited from another class). | Try it |
You can use one of the following attributes or methods:
Modifier | Overview |
---|
final | It is not possible to override or modify attributes and methods. |
static | Rather than belonging to an object, attributes and methods belong to a class. |
abstract | Usable only on abstract classes, and can only be implemented on methods. There is no body to the method, for example abstract void read();. Subclasses (the inherited class) provide the body. |
transient | When serializing an object, attributes and methods are skipped |
synchronized | There can only be one thread accessing a method at a time. |
volatile | An attribute’s value is not cached thread-locally, but instead is read from the “main memory.” |
Final
When it comes to Java modifiers, declare attributes as final if you don’t want them to override existing values:
public class Main {
final int mrx = 410;
final String ample = "Final";public static void main(String[] args) {
Main my_Object = new Main();
my_Object.mrx = 1020; // The final variable cannot be re-assigned a new value, so an error will be generated
my_Object.ample="Modified"; // The final variable cannot be re-assigned a new value, so an error will be generatedSystem.out.println(my_Object.mrx);
System.out.println(my_Object.ample);}
}
public class Main {final int minutes_in_an_hour=60; // Assigning values to the variable
public static void main(String[] args) {Main my_object=new Main();my_object.minutes_in_an_hour=23; // not possible to assign/ modify value of the variable as final keyword is assigned.System.out.println(my_object.minutes_in_an_hour);
}
}
Static
If it comes to Java modifiers, a static method can be invoked without initializing an object of the class.
To illustrate the difference between static and public methods, here is an example:
public class Main {
// Static method is created here
static void mrx_method(String mrx_bloodgroup){
System.out.println("Blood Group: "+mrx_bloodgroup);
}public void print_mrx_num(int mrx_limit){ // The public print_method is initialized here which prints numbers upto the limit given by userfor(int m_r=1;m_r<=mrx_limit;m_r++){System.out.println(m_r);}
}// Main method
public static void main(String[ ] args) {
mrx_method("B+"); // Make a call to the static method
// power_calculator(); An error will be generated as an outputMain my_Object = new Main(); // An object of Main Class is created here
my_Object.print_mrx_num(6); // The public method is called by using object of the class
}
/* Outputs =Blood Group: B+1
2
3
4
5
6
*/
}
public class Main {
// Static method
static void mrx_method(int mrx,int ample){
System.out.println("Product: "+(mrx*ample));
}// Public method
public void ample_method(){
System.out.println("Sum: "+(40+23));
}public static void main(String[ ] args) {
mrx_method(10,20); // Make a call to the static method
// ample_method() An error will be generated as an outputMain my_Object = new Main(); // An object of Main Class is created here
my_Object.ample_method(); // The public method is called by using object of the class
}// Output 1: Product: 200
// Output 2: Sum: 63
}
Abstract
When it comes to Java modifiers, abstract methods belong to abstract classes and do not have bodies.
Subclasses provide the body:
// Code is extracted from filename: Main.java
// an abstract class is created below
abstract class Main {
public String f_name = "Elon Musk";
public int age = 51;
public abstract void bio(); // an abstract method is created with does not have a body
}// Subclass (inherit from Main)
class Information extends Main {
public String date_of_birth= "June 28, 1971";
public void bio() { // Here is the abstract method's body
System.out.println("Elon Musk is the Owner of Tesla Cars");
}
}
// End code from filename: Main.java// Code from Second filename: Second.java
class Second {
public static void main(String[] args) {
// A new object of the Information class will be created (which inherits attributes and methods from Main).Information myObj = new Information();System.out.println("Name: " + myObj.f_name);
System.out.println("Age: " + myObj.age);
System.out.println("Date of Birth: " + myObj.date_of_birth);
myObj.bio(); // an abstract method is called here
}
//
// Output:
//
// Name: Elon Musk
// Age: 51
// Date of Birth: June 28, 1971
// Elon Musk is the Owner of Tesla Cars
//
}
// an abstract class is created below
abstract class Abstract_class {
public String mrx_name = "Abstract_Class";
public abstract void sum(); // an abstract method is created with does not have a body
}// Subclass (inherited from Abstract_class)class Third extends Abstract_class{public void sum() { // Abstract method body is defined here
System.out.println("Sum: "+(12+21));
}
}// End code from filename: Third.java// Code from Second filename: Second.java
class Second {
public static void main(String[] args) {
// A new object of the Information class will be created (which inherits attributes and methods from Main).Third my_Obj = new Third();
System.out.println("Topic: "+my_Obj.mrx_name);
my_Obj.sum(); // an abstract method is only called here
}// Output : Sum: 33
// Output: Topic: Abstract_Class
}