Java modifiers are keywords that you use to change the properties of classes, methods, and variables. They
define the accessibility, behavior, and scope of these components. Modifiers are divided into two categories:
access modifiers and non-access modifiers.
Access Modifiers
Access modifiers control the visibility of classes, methods, and variables. The four access modifiers in Java
are:
public: The member is accessible from any other class.
protected: The member is accessible within its own package and by subclasses.
default (no keyword): The member is accessible only within its own package.
private: The member is accessible only within its own class.
In this example, publicVar is accessible from anywhere, protectedVar is accessible
within the package and subclasses, defaultVar is accessible within the package, and
privateVar is accessible only within the Example class.
Non-Access Modifiers
Non-access modifiers provide additional functionalities such as controlling inheritance, ensuring immutability,
and defining special properties. Some common non-access modifiers are:
static: Indicates that the member belongs to the class, rather than instances of the
class.
final: Used to declare constants, prevent method overriding, and inheritance.
abstract: Used to declare abstract classes and methods.
synchronized: Used in multithreading to ensure that a method or block is accessed by only
one thread at a time.
volatile: Ensures that the value of a variable is always read from the main memory.
transient: Prevents serialization of class fields.
Example of Non-Access Modifiers
publicclassNonAccessExample{publicstaticfinalintCONSTANT=100;publicstaticvoidmain(String[] args){System.out.println("Constant Value: "+CONSTANT);}}Obsługiwane przez
In this example, CONSTANT is a static and final variable, meaning it belongs to the class and
cannot be changed.
Tips and Best Practices
Use Access Modifiers Wisely: Restrict access as much as possible to protect your data. Use
private for fields unless they need to be accessed outside the class.
Static Usage: Use static for utility methods and constants that do not require
object state.
Final for Constants: Declare constants using final and static to
prevent modification.
Abstract Classes: Use abstract classes as base classes that should not be
instantiated directly.
Synchronized for Thread Safety: Use synchronized to ensure thread safety when
necessary, but be aware of potential performance impacts.
Volatile for Visibility: Use volatile for variables shared between threads to
ensure visibility of changes.
Transient for Serialization: Use transient to exclude fields from serialization
when they should not be part of the serialized object.
Java modifiers are keywords that you use to change the properties of classes, methods, and variables. They define the accessibility, behavior, and scope of these components. Modifiers are divided into two categories: access modifiers and non-access modifiers.
Access Modifiers
Access modifiers control the visibility of classes, methods, and variables. The four access modifiers in Java are:
Example of Access Modifiers
In this example,
publicVaris accessible from anywhere,protectedVaris accessible within the package and subclasses,defaultVaris accessible within the package, andprivateVaris accessible only within theExampleclass.Non-Access Modifiers
Non-access modifiers provide additional functionalities such as controlling inheritance, ensuring immutability, and defining special properties. Some common non-access modifiers are:
Example of Non-Access Modifiers
In this example,
CONSTANTis a static and final variable, meaning it belongs to the class and cannot be changed.Tips and Best Practices
privatefor fields unless they need to be accessed outside the class.staticfor utility methods and constants that do not require object state.finalandstaticto prevent modification.abstractclasses as base classes that should not be instantiated directly.synchronizedto ensure thread safety when necessary, but be aware of potential performance impacts.volatilefor variables shared between threads to ensure visibility of changes.transientto exclude fields from serialization when they should not be part of the serialized object.