Basic use of modifiers:
Access Modifiers(public,protected,default[package access],private)
Comparison between access modifiers
Non-Access Modifiers(transient,volatile,final,abstract,native,synchronized,static,strictfp)
Impact of final keyword:
If you use keyword final with top-level class, you can't create a subclass of this class.
Example:
If you use the keyword final with an instance variable, you must initialize this variable before constructor completion otherwise you will get a compiler error.
Following are the various possible places for the initialization
1.At the time of declaration
Example:
Example:
Example:
Note: Once the final variable is initialized you can't change it.
final instance variable become compile time constant.
If you use keyword final with static variable, you must initialize this variable before class loading completion otherwise, you will get a compiler error.
The following are the various possible places for the initialization.
1.At the time of declaration
Example:
Example:
Note: The final is the only modifier that is applicable for local variables and method parameters.
abstract keyword
Access Modifiers(public,protected,default[package access],private)
Comparison between access modifiers
Visibility
|
private
|
default
|
protected
|
public
|
1.Within the same class
|
Yes
|
Yes
|
Yes
|
Yes
|
2.From child class of the same package
|
No
|
Yes
|
Yes
|
Yes
|
3.From the non-child class of the same package
|
No
|
Yes
|
Yes
|
Yes
|
4.From child class of outside package
|
No
|
No
|
Yes, But you must use child class reference only
|
Yes
|
5.From the non-child class of outside package
|
No
|
No
|
No
|
Yes
|
Impact of final keyword:
If you use keyword final with top-level class, you can't create a subclass of this class.
Example:
- public final class A{}
- public class B extends A{} // Compiler Error.
If you use the keyword final with an instance variable, you must initialize this variable before constructor completion otherwise you will get a compiler error.
Following are the various possible places for the initialization
1.At the time of declaration
Example:
- final int a =10;
Example:
- final int a;
- {a = 10;}
Example:
- class Test{
- final int a;
- Test(){
- a =10;
- }
- }
Note: Once the final variable is initialized you can't change it.
final instance variable become compile time constant.
If you use keyword final with static variable, you must initialize this variable before class loading completion otherwise, you will get a compiler error.
The following are the various possible places for the initialization.
1.At the time of declaration
Example:
- final static int a =10;
Example:
- final static int a;
- static {a = 10;}
Note: The final is the only modifier that is applicable for local variables and method parameters.
abstract keyword
- You can use the keyword abstract with top-level classes and methods only.
- You can't use the keyword abstract with variables.
- If you use the keyword abstract with class, you can't create an object of this class.
- If you declare an abstract method(method without body) in a class, you must make that class abstract otherwise you will get a compiler error.
- It is applicable for variables, methods, and blocks
- If you use keyword static with variables, then initialization happens only once and you can share the same copy throughout the application.
- If you use keyword static with methods, you can't override this method.
- It is applicable only for methods.
- The methods which are implemented in non-java is called native method.
- It is only applicable to variables.
- If you use keyword transient with an instance variable, this variable won't be serialized.
- If you use keyword transient with static variables, there is no impact. Because the static variable is not part of the object.
- If you use keyword transient with the final variable, there is no impact. Because final variables are by default eligible for serialization.
- It is only applicable to variables.
- You can solve the visibility problem by using keyword volatile in a multithreaded environment.
- You can use the keyword synchronized with methods and blocks only.
- You can solve the data inconsistency problem by using the keyword synchronized in a multithreaded environment.
- You can use keyword stricfp with classes, interfaces, and methods only.
- To ensure that calculations using floating-point numbers are identical on all platforms.