What are access specifiers and access modifiers in java
Access specifiers Access modifiers
Public Public
Protected Abstract
Private Final
Static
Volatile
Synchronized
Transient
Native
- Public : The Variables and methods can be access any where and any package.
- Protected : The Variables and methods can be access same Class, same Package & sub class.
- Private : The variable and methods can be access in same class only.
Same class - Public, Protected, and Private
Same-package & subclass - Public, Protected
Same Package & non-sub classes - Public, Protected
Different package & Sub classes - Public, Protected
Different package & non- sub classes - Public
Same-package & subclass - Public, Protected
Same Package & non-sub classes - Public, Protected
Different package & Sub classes - Public, Protected
Different package & non- sub classes - Public
Identifiers : are the Variables that are declared under particular Data type.
Literals: are the values assigned to the Identifiers.
Static : access modifier. Signature: Variable-Static int b; Method- static void meth(int x)
When a member is declared as Static, it can be accessed before any objects of its class are
created and without reference to any object. Eg : main(),it must call before any object exit.
- Static can be applied to Inner classes, Variables and Methods.
- Local variables can’t be declared as static.
- A static method can access only static Variables. and they can’t refer to this or super in anyway.
- Static methods can’t be abstract.
- A static method may be called without creating any instance of the class.
Only one instance of static variable will exit any amount of class instances.
Final : access modifier
Final : access modifier
- All the Variables, methods and classes can be declared as Final.
- Classes declared as final class can’t be sub classed.
- Method ‘s declared as final can’t be over ridden.
- If a Variable is declared as final, the value contained in the Variable can’t be changed.
- Static final variable must be assigned in to a value in static initialized block.
Transient : access modifier
- Transient can be applied only to class level variables.
- Local variables can’t be declared as transient.
- During serialization, Object’s transient variables are not serialized.
- Transient variables may not be final or static. But the complies allows the declaration and no compile time error is generated.
Volatile : access modifier
- Volatile applies to only variables.
- Volatile can applied to static variables.
- Volatile can not be applied to final variables.
- Transient and volatile can not come together.
- Volatile is used in multi-processor environments.
Native : access modifier
- Native applies to only to methods.
- Native can be applied to static methods also.
- Native methods can not be abstract.
- Native methods can throw exceptions.
- Native method is like an abstract method. The implementation of the abstract class and native method exist some where else, other than the class in which the method is declared.
Synchronized : access modifier
- Synchronized keyword can be applied to methods or parts of the methods only.
- Synchronize keyword is used to control the access to critical code in multi-threaded programming.
Declaration of access specifier and access modifiers
No comments:
Post a Comment