Access Modifier / specifiers in Java
modifiers : A modifier in java is a keyword that we add to those definitions that we need to change their meaning.
There are two types of modifiers in Java:
access modifiers and non-access modifiers.
Accesse Modifiers :
Access modifiers/specifiers in Java define the boundary for accessing members of a class and a class itself. OR
access modifiers are those modifiers that are used to restrict the visibility (accessibility) of classes, fields, methods, or constructors.
In Java, access modifiers or access specifiers are keywords used to define the accessibility or visibility of classes, variables, methods, and constructors. They control how these elements can be accessed or interacted with by other parts of the program. Java provides four types of access modifiers:
1. Public : The public access modifier allows access to the within a class and outside of class , method, variable, or constructor from any other part of the program, including other classes and packages.
2. private :The `private` access modifier restricts access to the class, method, variable, or constructor within its own class. It means that the element declared as `private` can only be accessed within the same class and not from outside or other classes.
3. protected :The `protected` access modifier allows access to the class, method, variable, or constructor within the same package and also in subclasses, even if they are in different packages. It is similar to the `default` access modifier (explained next) but provides additional access to subclasses.
4. default (no modifier) : If no access modifier is specified, it is considered as the default access level. The default access allows access to the class, method, variable, or constructor within the same package but restricts access from other packages. In other words, elements with the default access modifier are accessible only within the same package.
Visibility of Access Modifier in Java
Access location | Public | Protected | Default | Private protected | Private |
---|---|---|---|---|---|
Same class | Yes | Yes | Yes | Yes | Yes |
Subclass in same package | Yes | Yes | Yes | Yes | No |
Other classes in same package | Yes | Yes | Yes | No | No |
Subclasses in other package | Yes | Yes | No | Yes | No |
Non-subclasses in other packages | Yes | No | No | No | No |
Non Access Modifiers in Java
Native Modifier in Java
1. A native modifier can be used only with a method. It contains only a signature but not a body.
2. A native method is generally used to merge other programming languages like C and C++ code into Java programming.
Strictfp Modifier in Java
1. A strictfp modifier can be applied with outer class, inner class, method, and outer interface. It cannot be applied with variable, block, inner interface, constructor, and enum.
2. A strictfp class uses the IEEE 754-1985 floating-point specification for all of its floating-point operations.
3. The method defined within the interface cannot be declared with strictfp modifier.
4. Stricfp modifiers cannot be used with native modifiers simultaneously.
Synchronized Modifier in Java
1. A synchronized modifier in Java can be applied only with method and block.
2. In the synchronized method block, only one thread is allowed to execute at a time. It makes thread-safe. Therefore, the synchronized method or synchronized block is mainly used for thread safety.
3. Statements can also be synchronized in Java.
Transient Modifier in Java
1. The transient modifier can be applied only with variables or data members.
2. When a class is serialized then transient data member is not serialized.
Volatile Modifier in Java
1. A volatile modifier can be applied only with variables. It is a keyword.
2. If a data member or variable is declared as volatile, all threads can see consistent the value of variable in java memory.
3. A volatile keyword cannot be applied with a method declaration.
4. Volatile does not create any kind of lock on the variable in the program.
Comments
Post a Comment