JAVA OBJECTS NON ACCESS MODIFIERS PART 3 FINAL
CLICK JAVA OBJECTS NON ACCESS MODIFIERS PART 3 FINAL
JAVA OBJECTS
NON ACCESS MODIFIERS
PART 3 FINAL
What is a FINAL MODIFIER ?
Why to use FINAL MODIFIER VARIABLES IN JAVA CODE ?
In Java, access and non-access modifiers are used to set access levels for classes, variables, methods, and constructors. Each modifier serves a distinct purpose:
- Public Modifier: The public access modifier allows the class, method, or field to be accessed from any other class in any package. It offers the least restriction on access level.
- Private Modifier: The private access modifier restricts the visibility to the declaring class only. It’s the most restrictive access level and is typically used for encapsulating data in classes.
- Protected Modifier: The protected access modifier allows access within the same package and subclasses in other packages. It provides a more controlled form of accessibility than public.
- Default Modifier: If no access modifier is specified, Java uses a default package-private access level. This allows the class, field, or method to be accessible only within its own package.
- Static Modifier: The static non-access modifier indicates that a member belongs to the class itself rather than to any specific instance of the class. Static members can be accessed without creating an instance of the class.
- Final Modifier: The final non-access modifier, when applied to a class, method, or variable, indicates that it cannot be further extended, overridden, or reassigned, respectively.
- Abstract Modifier: The abstract modifier can be used with classes and methods. An abstract class cannot be instantiated, and an abstract method must be implemented in a subclass.
- Transient Modifier: The transient non-access modifier is used in serialization. It indicates that a field should not be serialized — for example, when a field contains sensitive information.
- Volatile Modifier: The volatile non-access modifier is used with variables. It tells the JVM that a thread accessing the variable must always merge its own private copy of the variable with the master copy in the memory.
- Synchronized Modifier: The synchronized non-access modifier, which can be applied to methods or blocks, indicates that only one thread at a time can execute the method or block, which is essential for thread safety in multi-threaded applications.
These modifiers in Java are crucial for implementing concepts like encapsulation, inheritance, and polymorphism, and they play a significant role in the design of robust and secure applications.