How does final modifier work?
Final in java is used to restrict user to change further. It is used in various context as below
Final variable
• As variable become final, its value will never be modified.
• The variables become read only variable forever.
• If final variable is static then it must be initialized at the time of declaration.
• A non-static final variable can be blank but must be initialized in constructor.
• A local variable can be final.
• Final variables usually come with static key word as constant.
Final method
• As method become final, it never be override but be overload.
• A final method can be static and synchronized but never be abstract.
• As we are sure that the method implementation is complete and will never be override then we should make it final.
Final class
• A final class is complete class and will never be sub-classed.
• Final class helps to define immutable class.
• There are some final classes like String, Integer and other wrapper classes.
| Is This Answer Correct ? | 5 Yes | 0 No |
Difference between nested and inner classes ?
What is the use of accept () method in java?
Can you call a private data from an inner class?
Does importing a package imports its sub-packages as well in java?
Does java return by reference?
Why java is said to be pass-by-value ?
What are the basic control structures?
What is the difference between static method and instance method in Java?
0 Answers SwanSoft Technologies,
How many types of parsers are there?
What is initial size of arraylist in java?
List java api that supports threads?
How can you set the applet size?