• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Java | Modifiers
  1. Class Modifiers
    1. Access Modifiers: public, package-private
    2. Other Modifiers: abstract, final
  2. Interface Modifiers
    1. Access Modifiers: public, package-private
    2. Other Modifiers: abstract
  3. Method Modifiers
    1. Access Modifiers: public, private, protected, package-private
    2. Other Modifiers: static, abstract, final, synchronized, strictfp, native
  4. Variable Modifiers
    1. Access Modifiers: public, private, protected, package-private
    2. Other Modifiers: static, final, transient, volatile

  1. Class Modifiers
    1. Access Modifiers: public, package-private
      A class can have two access levels: public and package-private (also called default access).
      • The public access level allows a class to be visible throughout the entire code with no restrictions.
        Anywhere in the code, a public class can be referenced, instantiated, and extended (through inheritance).
        public class MyPublicClass {}
      • The package-private access level allows a class to be visible only within its own package.
        There is no explicit modifier for this access level; in fact, if no modifier is used in the class declaration, the class is given the default package-private access level.
        package com.mtitek.modifiers;
        
        class MyDefaultClass {}
    2. Other Modifiers: abstract, final
      • The abstract modifier:
        An abstract class is a template class that declares methods (which may also be abstract). It allows other classes to use it (by inheriting from this class) to define specialized behavior.
        An abstract class cannot be instantiated directly.
        An abstract class cannot use the final modifier.
        public abstract class MyAbstractClass {}
      • The final modifier:
        The final keyword prevents a class from being extended, which means its behavior cannot be modified through inheritance.
        A final class cannot use the abstract modifier.
        public final class MyFinalClass {}
    Notes:

    Classes cannot be private.
    You will get a compilation error if you mark a class as private.

  2. Interface Modifiers
    1. Access Modifiers: public, package-private
      An interface can have two access levels: public and package-private.
      • The public access level allows an interface to be visible throughout the entire code with no restrictions.
        Anywhere in the code, a public interface can be referenced and implemented (through inheritance).
        public interface MyPublicInterface {}
      • The package-private access level allows an interface to be visible only within its own package.
        There is no explicit modifier for this access level; in fact, if no modifier is used in the interface declaration, the interface is given the default package-private access level.
        package com.mtitek.modifiers;
        
        interface MyDefaultInterface {
        }
    2. Other Modifiers: abstract
      • The abstract modifier:
        Interfaces are implicitly abstract.
        This means that it is not necessary to use this modifier when declaring interfaces.
        public abstract interface MyAbstractPublicInterface {}
    Notes:

    Interfaces cannot be private.
    You will get a compilation error if you mark a interface as private.

    Interfaces cannot be final.
    You will get a compilation error if you mark an interface as final.

    Interface variables are implicitly public, static, and final.
    This means that it is not necessary to use these modifiers when declaring variables in interfaces.
    However, it is forbidden to use any other modifier.

    Interface methods have different default modifiers depending on their type:
    Abstract methods are implicitly public and abstract.
    Default methods are implicitly public.
    Static methods are implicitly public.
    Private methods must be explicitly marked as private.

  3. Method Modifiers
    1. Access Modifiers: public, private, protected, package-private
      A method can have four access levels: public, private, protected, and package-private.

      • The public access modifier allows a method to be accessible throughout the code without any restrictions (provided that the class declaring it is also visible).

      • The private access modifier allows a method to be accessible only within the code of the class that declares it.
        The private access does not prevent a subclass from declaring a method with the exact same signature. In this case, we are not talking about overriding the method (inheritance) but rather defining a new method since the superclass method is not visible to the subclass.

      • The protected access modifier allows a method to be visible within the package of the class that declares it.
        It also allows the method to be visible in the code of subclasses of the class that declares it, even if they are in different packages.

        Be aware that the method in the parent class is accessible in the subclass code as if it had been declared in the subclass itself: It can be overridden or invoked.

        However, it will not be possible for the subclass code to use a reference of an instance of the parent class to invoke the protected method when accessing it from a different package.
        package com.mtitek.inheritance.package1;
        
        public class MyParentClass {
            protected void doFirstThing() {
                // ...
            }
        
            protected void doSecondThing() {
                // ...
            }
        }
        package com.mtitek.inheritance.package2;
        
        import com.mtitek.inheritance.package1.MyParentClass;
        
        public class MySubClass extends MyParentClass {
            @Override
            protected void doFirstThing() {
                super.doFirstThing();
            }
        
            public void doThirdThing() {
                doFirstThing(); // OK
                doSecondThing(); // OK
        
                MyParentClass myParentClass = new MyParentClass();
                myParentClass.doFirstThing(); // Compiler error: The method doFirstThing() from the type MyParentClass is not visible
            }
        }
      • The package-private access modifier allows a method to be visible only within the package of the class that declares it.
        There is no explicit modifier for this access; in fact, in the absence of a modifier in the method declaration, it defaults to package-private access.

      Below is a table summarizing the visibility of these methods based on their access modifiers, and also depending on the context in which they are invoked (considering a class T that declares methods with the access modifiers public, private, protected, and package-private) :

      Visibility of methods in class T public private protected package-private
      In the code of class T Yes Yes Yes Yes
      In the code of any class (other than class T) in the same package as class T Yes No Yes Yes
      In the code of any class (not a subclass) in a different package from class T Yes No No No
      In the code of any subclass in the same package as class T Yes No Yes Yes
      In the code of any subclass in a different package from class T Yes No Yes No
    2. Other Modifiers: static, abstract, final, synchronized, strictfp, native
      • The static modifier:
        A static method can be invoked without creating an instance of the class; a static method is accessible when the class is loaded into memory by the JVM.
        A static method cannot use instance variables or invoke instance methods directly (without an object reference).

      • The abstract modifier:
        An abstract method is a template method that does not implement code; it allows other classes to use it (by inheriting from the class that defines this method) to define specialized behavior.
        An abstract method can only be defined in an abstract class or interface.
        An abstract method can have three access levels: public, protected, and package-private.
        An abstract method cannot use the final, private, or static modifiers.
        When a concrete class inherits from an abstract class, it must implement all the abstract methods from the class.
        public abstract class MyAbstractClass {
            public abstract void doSomething();
        }
      • The final modifier:
        The final keyword prevents a method from being overridden; this means that its behavior cannot be changed in subclasses.
        public class MyClass {
            public final void doSomething() {
                // add code here
            }
        }
      • The synchronized modifier:
        The synchronized keyword ensures that a method is executed by only one thread at a time.
        public class MyClass {
            public synchronized void doSomething() {
                // add code here
            }
        }
      • The strictfp modifier:
        The strictfp keyword ensures that a method complies with the IEEE 754 standard for floating-point number handling.

        Some processors might not truncate intermediate results of a computation, which can increase the precision of the result but also may lead to different results depending on the processor executing the computation.

        If portability is an issue, consider using the strictfp keyword to ensure that all instructions (within a method or a class) use strict floating-point computations.

        When using mathematical functions, you might consider using the StrictMath class (instead of Math) if you expect results to be the same regardless of the processor used to execute the mathematical functions (this might impact performance, though).
        public class MyClass {
            public strictfp void doSomething() {
                // add code here
            }
        }
      • The native modifier:
        The native keyword indicates that the method is implemented in another language (e.g., C or C++).
        public class MyClass {
            public native void doSomething();
        }
  4. Modifiers of Variables
    Note: Local variables (declared inside methods) can only use the final modifier.
    1. Access Modifiers: public, private, protected, package-private
      A variable can have four access levels: public, private, protected, and package-private.
      Access modifiers only apply to instance and class variables.
      Access modifiers do not prevent a subclass from declaring a variable with the same name as a superclass variable and specifying a different access modifier.

      Variable hiding (not inheritance) occurs when a subclass declares a variable with the same name as a superclass variable.

      • The public access modifier allows a variable to be accessible throughout the entire code, without any restrictions, as long as the class declaring the variable is also visible.

      • The private access modifier allows a variable to be accessible only within the code of the class that declares it.

      • The protected access modifier allows a variable to be visible within the package of the class that declares it.
        It also allows the variable to be visible in the code of subclasses of the class that declares it, even if those subclasses are declared in other packages.

        However, note that the parent class's variable is accessible in the subclass code as if it were declared in the subclass.
        On the other hand, it will not be possible for the subclass code to use a reference to an instance of the parent class to reference the protected variable when the reference and the subclass are in different packages.
        package com.mtitek.inheritance.package1;
        
        public class MyParentClass {
            protected Integer var1;
        }
        package com.mtitek.inheritance.package2;
        
        import com.mtitek.inheritance.package1.MyParentClass;
        
        public class MySubClass extends MyParentClass {
            public void doSomething() {
                var1 = 10; // OK
        
                MyParentClass myParentClass = new MyParentClass();
                myParentClass.var1 = 10; // Compiler error: The field MyParentClass.var1 is not visible
            }
        }
      • The package-private access modifier allows a variable to be visible only within the package of the class that declares it.
        There is no explicit modifier for this access level; in fact, in the absence of any modifier in the variable declaration, it defaults to package-private access.
    2. Other Modifiers: static, final, transient, volatile
      • The static modifier:
        The static keyword means that the variable belongs to the class rather than to any instance of the class, so there is only one copy shared by all instances.
        A static variable can be accessed without creating an instance of the class; a static variable is accessible after the class is loaded into memory by the JVM.

      • The final modifier:
        The final keyword prevents the modification of the reference contained in a variable (applies to class variables, instance variables, local variables inside methods, and method parameters).
        public class MyClass {
            public static final Integer ADD_OPERATION_ID = 0;
        }
      • The transient modifier:
        The transient keyword indicates that the variable should not be considered during the serialization of the object that contains the variable.

      • The volatile modifier:
        The volatile keyword ensures that reads and writes to the variable are performed directly on main memory, making the variable's value visible to all threads immediately.

        There are specific cases where this keyword can work correctly. It should not be used where reading and modifying the variable needs to be an atomic operation (for example, incrementing the value of a variable).
        Also, care should be taken when the variable is used in an operation involving another variable (for example, checking if the value of the variable is equal to or different from the value of another variable).

        Typically, the variable marked with this keyword should be written to in one thread and then read by other threads to check for changes in its value.
© 2025  mtitek