public
and package-private
(also called default access).public
access level allows a class to be visible throughout the entire code with no restrictions.public
class can be referenced, instantiated, and extended (through inheritance).public class MyPublicClass {}
package-private
access level allows a class to be visible only within its own package.package-private
access level.package com.mtitek.modifiers; class MyDefaultClass {}
abstract
modifier:final
modifier.public abstract class MyAbstractClass {}
final
modifier:final
keyword prevents a class from being extended, which means its behavior cannot be modified through inheritance.final
class cannot use the abstract
modifier.public final class MyFinalClass {}
Classes cannot be private
.
You will get a compilation error if you mark a class as private
.
public
and package-private
.public
access level allows an interface to be visible throughout the entire code with no restrictions.public
interface can be referenced and implemented (through inheritance).public interface MyPublicInterface {}
package-private
access level allows an interface to be visible only within its own package.package-private
access level.package com.mtitek.modifiers; interface MyDefaultInterface { }
abstract
modifier:abstract
.public abstract interface MyAbstractPublicInterface {}
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
.
public
, private
, protected
, and package-private
.public
access modifier allows a method to be accessible throughout the code without any restrictions (provided that the class declaring it is also visible).private
access modifier allows a method to be accessible only within the code of the class that declares it.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.protected
access modifier allows a method to be visible within the package of the class that declares it.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 } }
package-private
access modifier allows a method to be visible only within the package of the class that declares it.package-private
access.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 |
static
modifier:abstract
modifier:public
, protected
, and package-private
.final
, private
, or static
modifiers.public abstract class MyAbstractClass { public abstract void doSomething(); }
final
modifier: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 } }
synchronized
modifier: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 } }
strictfp
modifier:strictfp
keyword ensures that a method complies with the IEEE 754 standard for floating-point number handling.strictfp
keyword
to ensure that all instructions (within a method or a class) use strict floating-point computations.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 } }
native
modifier:native
keyword indicates that the method is implemented in another language (e.g., C or C++).public class MyClass { public native void doSomething(); }
final
modifier.public
, private
, protected
, and package-private
.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.private
access modifier allows a variable to be accessible only within the code of the class that declares it.protected
access modifier allows a variable to be visible within the package of the class that declares it.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 } }
package-private
access modifier allows a variable to be visible only within the package of the class that declares it.package-private
access.static
modifier: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.final
modifier: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; }
transient
modifier:transient
keyword indicates that the variable should not be considered during the serialization of the object that contains the variable.volatile
modifier: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.