Hello friends,
Welcome back 🙂.
In this article, I'm going to give a short introduction to final keyword which would be helpful to know more about it.
The final keyword in java has multiple uses. The main purpose of the final keyword is to add some restrictions on java elements (class, member and method). The restrictions have been described as below.
Use of final keyword:
1. The final class : We can say the class is final when it declared as final. The restriction on final class is that the final class cannot be extended by any class. Rest of the behavior of the class remains same as any normal class.
2. The final method : The method declared using final keyword is to be said as final method. The restriction is that the final method cannot be overridden by derived class. This restriction applies to static method also.
3. The final member : The final member is the java class's member which is declared using final keyword. The restriction is that it can be initialized only once either it is static or non-static.
Notes:
1. We must assign value to final members, if not so, compiler will throw compile time error. The final variable can be assigned in either static block (only if final variable is static), or in initializer block (only if final variable is non-static i.e. instance variable), or in constructor or at the time of declaration.
2. Only non-static final variables/members can be assigned in the initializer block, because the initializer block always execute when object of that class is created.
3. Only static final variables/members can be assigned in the static block, because the static block executes only once when the class is loaded into the memory by class loader. The static final variables cannot be assigned in constructor.
4. A final member can only be assigned once and a final member can only be assigned in either initializer block or constructor or inline. This restriction is because java compiler checks the flow of code to ensure that the final variable must be assigned only once.
5. If we use final keyword for an object which holds a reference to an instance of a class, then the state of that object or instance can be changed by methods of that object, but the variable which holds that instance always refer to the same instance or object. To understand this, look into the following code segment:
Hope this is helpful. Let me know in comment if anything is missing or any queries.
Thanks
Girish
Welcome back 🙂.
In this article, I'm going to give a short introduction to final keyword which would be helpful to know more about it.
The final keyword in java has multiple uses. The main purpose of the final keyword is to add some restrictions on java elements (class, member and method). The restrictions have been described as below.
Use of final keyword:
1. The final class : We can say the class is final when it declared as final. The restriction on final class is that the final class cannot be extended by any class. Rest of the behavior of the class remains same as any normal class.
2. The final method : The method declared using final keyword is to be said as final method. The restriction is that the final method cannot be overridden by derived class. This restriction applies to static method also.
3. The final member : The final member is the java class's member which is declared using final keyword. The restriction is that it can be initialized only once either it is static or non-static.
Notes:
1. We must assign value to final members, if not so, compiler will throw compile time error. The final variable can be assigned in either static block (only if final variable is static), or in initializer block (only if final variable is non-static i.e. instance variable), or in constructor or at the time of declaration.
2. Only non-static final variables/members can be assigned in the initializer block, because the initializer block always execute when object of that class is created.
3. Only static final variables/members can be assigned in the static block, because the static block executes only once when the class is loaded into the memory by class loader. The static final variables cannot be assigned in constructor.
4. A final member can only be assigned once and a final member can only be assigned in either initializer block or constructor or inline. This restriction is because java compiler checks the flow of code to ensure that the final variable must be assigned only once.
5. If we use final keyword for an object which holds a reference to an instance of a class, then the state of that object or instance can be changed by methods of that object, but the variable which holds that instance always refer to the same instance or object. To understand this, look into the following code segment:
Hope this is helpful. Let me know in comment if anything is missing or any queries.
Thanks
Girish
Comments
Post a Comment
Want to share something? Add here...