What is blank final variable in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

What is blank final variable in java?

Can anyone explain what is blank final variable and why it is useful?

13th Aug 2020, 8:08 AM
Raina Dhankhar
Raina Dhankhar - avatar
3 Answers
+ 11
Sometimes we don’t know the value of the final variable during the creation of the class. The value could be decided during the construction time of the object. If we want to create a constant value that will be common for all the objects, then we should initialize the final variable during the declaration. But there may be a situation when we want to create a constant value that will not be common for all objects. We want to make it constant only within the object. When a constructor will initialize the blank final variable then it will create a new copy for each object. Suppose you are providing login functionality on your website. A user can set the username only when he/she is creating the account on the website. After that, it should not be changed. Here you can create a blank final variable of username. Each user will provide the username during the creation of the account. For more details: https://javagoal.com/blank-final-variable-in-java/
13th Aug 2020, 8:47 AM
Raina Dhankhar
Raina Dhankhar - avatar
+ 8
A blank final variable is used with a class when you have a property that you want to be set once and only once so that it can not change. The final keyword, when used with a variable, makes the variable so that its value cannot be altered. A final variable may only be assigned once and must be assigned when declared with the exception of a blank final variable. A blank final variable is set in the constructor for the class. They can be useful for things such as database connections or similar values or objects that could be harmful if changed during runtime. https://www.geeksforgeeks.org/blank-final-in-java/
13th Aug 2020, 8:23 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Raina Dhankhar please don't ask a question and turn around and answer your question. This is not properly using the question and answer section. Please follow the guidelines. Thanks and happy coding. https://www.sololearn.com/discuss/1316935/?ref=app
17th Nov 2020, 11:55 PM
BroFar
BroFar - avatar