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:

public class Main

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:

ModifierOverviewTry it
publicAccess to the class is possible from any other class.Try it
defaultClasses 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:

ModifierOverviewTry it
publicAll classes have access to the code.Try it
privateA declared class is the only place where the code can be accessed.Try it
defaultAccess to the code is limited to the same package. In the absence of a modifier, this is used.Try it
protectedIn 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’sWithin ClassWithin PackageOutside Package (subclass only)Outside Package
PrivateYNNN
DefaultYYNN
ProtectedYYYN
PublicYYYY

Non-Access Modifiers

You can use either an abstract or a final modifier for classes:

ModifierOverviewTry it
finalA class can’t be inherited by another class if it contains the final modifier.Try it
abstractObjects 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:

ModifierOverview
finalIt is not possible to override or modify attributes and methods.
staticRather than belonging to an object, attributes and methods belong to a class.
abstractUsable 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.
transientWhen serializing an object, attributes and methods are skipped
synchronizedThere can only be one thread accessing a method at a time.
volatileAn 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:

Example: 

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);} }

Example: 

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:

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 */ }

Example: 

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:

Example: 

// 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 // }

Example: 

// 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 }

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *