Modifiers In Java – Access Modifiers And Non- Access Modifiers

What are Modifiers in Java?

The Modifiers In Java, these are words that change the meanings of definitions by being added to them.

There are many kinds of modifiers in the Java language.

You are probably already familiar with the word “public,” which shows up in almost all of our examples:

public class Main

The public keyword is an Access Modifier, which means that it tells classes, attributes, methods, and constructors what level of access they have.

Two Types Of Modifiers In Java

Access Modifiers

The Access Modifiers In Java are used to set how classes, interfaces, variables, methods, constructors, data members, and setter methods can be used (how visible they are).

You can use either public or default for a class:

ModifierDescription
publicThe class can be accessed by any other class.
defaultOnly classes in the same package can use the class. This is used when no modifier is given. In the Packages chapter, you’ll learn more about packages.
public access modifiers

You can use any of the following for attributes, methods, and constructors:

ModifierDescription
publicThe code can be used by all classes.
privateThe code can only be used within the class that was declared.
defaultThe code is only accessible within the same package. This is what you use when you don’t specify a modifier. You’ll learn more about packages in the Packages chapter.
protectedThe code is accessible from the same package and subclasses. The Inheritance chapter has more information about subclasses and super classes.
attributes, methods, and constructors access modifiers

Non-Access Modifiers

The Non-Access Modifiers In Java are keywords that were added to Java 7 to tell the JVM about how a class acts, what methods it has, what variables it has, etc.

This helps add more features, like the last keyword, which tells the computer that the variable can’t be set more than once.

You can use either the final or abstract for classes:

ModifierDescription
publicOther classes can’t use this class as a base. (The Inheritance chapter will tell you more about inheritance.)
abstractObjects can’t be made using this class. (To use an abstract class, you must inherit it from another class. The Inheritance and Abstraction chapters have more information about inheritance and abstraction.
public non-access modifiers

You can use any of the following for attributes and methods:

ModifierDescription
finalAttributes and methods can’t be overridden or changed.
staticAttributes and methods belong to the class, not to an object.
abstractCan only be used on methods and only in abstract classes. The body of the method is missing, like in abstract void run();. The subclass is in charge of the body (inherited from). In the Inheritance and Abstraction chapters, you will learn more about inheritance and abstraction.
transientAttributes and methods are left out when an object that has them is serialized.
synchronizedOnly one thread at a time can access a method.
volatileThe value of an attribute is never cached on a single thread, but is always read from the “main memory.”
attributes and methods non-access modifiers

In order for you to test the Java code provided in this lesson, you must test the code in your code editor.

You can test the above example here! ➡Java Online Compiler 

We also provide a Java Online Compiler for every code below. Just scroll down so that you can easily test or execute the source code.

Final Modifier in Java

The Final Modifiers In Java is a non-access modifier that makes classes, methods, and attributes unchangeable (impossible to inherit or override).

When you want a variable to always hold the same value, like PI, you can use the final keyword (3.14159…). “Modifier” is the word for the last keyword.

Example of Final Modifier Code:
public class Main {
  final int x = 30;
  final double PI = 5.14;

  public static void main(String[] args) {
    Main myObj = new Main();
    myObj.x = 60; // will generate an error: cannot assign a value to a final variable
    myObj.PI = 55; // will generate an error: cannot assign a value to a final variable
    System.out.println(myObj.x);
  }
}

Output:

/Main.java:7: error: cannot assign a value to final variable x
myObj.x = 60; // will generate an error: cannot assign a value to a final variable
^
/Main.java:8: error: cannot assign a value to final variable PI
myObj.PI = 55; // will generate an error: cannot assign a value to a final variable
^
2 errors

You can test the above example here! ➡Java Online Compiler 

Static Modifier In Java

The Static Modifiers In Java is called a non-access modifier.

The static modifier makes a member (variables or methods) of a class independent of the objects in the class.

It is used to define properties that are the same for all objects in the class.

An example of how static methods are different from public methods:

Example of Static Modifier Code:
public class Main {
  // Static method
  // Created By Glenn Magada Azuelo
  static void myStaticMethod() {
    System.out.println("Static methods can be called without creating objects");
  }

  // Public method
  public void myPublicMethod() {
    System.out.println("Public methods must be called by creating objects");
  }

  // Main method
  public static void main(String[ ] args) {
    myStaticMethod(); // Call the static method
    // myPublicMethod(); This would output an error

    Main myITSC = new Main(); // Create an object of Main
    myITSC.myPublicMethod(); // Call the public method
  }
}

Output:

Static methods can be called without creating objects
Public methods must be called by creating objects

You can test the above example here! ➡Java Online Compiler 

Abstract Modifier In Java

The Abstract Modifiers In Java is a non-access modifier in Java that can be used with classes and methods but not with variables.

It is used to achieve abstraction, which is one of the main ideas behind Object-Oriented Programming (OOP).

Here are some of the ways that Java’s abstract can be used.

Abstract methods are part of abstract classes and don’t have bodies. The subclass is in charge of the body:

// Code from filename: Main.java
// abstract class
// Created By Glenn Magada Azuelo
abstract class Main {
  public String fname = "Glenn";
  public int age = 26;
  public abstract void study(); // abstract method
}

// Subclass (inherit from Main)
public class Student extends Main {
  public int graduationYear = 2022;
  public void study() { // the body of the abstract method is provided here
    System.out.println("Studying all day long");
  }
}
// End code from filename: Main.java

// Code from filename: Second.java
class Second {
  public static void main(String[] args) {
    // create an object of the Student class (which inherits attributes and methods from Main)
    Student myObj = new Student();

    System.out.println("Name: " + myObj.fname);
    System.out.println("Age: " + myObj.age);
    System.out.println("Graduation Year: " + myObj.graduationYear);
    myObj.study(); // call abstract method
  }
}

Output:

Name: Glenn
Age: 26
Graduation Year: 2023
Studying all day long

Conclusion

In this article, we talked about Modifiers In Java, how it works, and how this chapter could help you learn Java.

This could be one of the most important learning guides that will help you improve your skills and knowledge as a future Java Developer.

What’s Next

The next section talks about Basic Operators In Java programming. At the end of the session, you’ll know what Operators are all about in Java.


Leave a Comment