I was looking at another question about final variables and noticed that you can declare final variables without initializing them (a blank final variable). Is there a reason it is desirable to do this, and when is it advantageous?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is useful to create immutable objects:
Bla is immutable (once created, it can’t change because color is final).
But you can still create various Blas by constructing them with various colors.
See also this question for example.
EDIT
Maybe worth adding that a “blank final” has a very specific meaning in Java, which seems to have created some confusion in the comments – cf the Java Language Specification 4.12.4:
You then must assign that blank final variable in a constructor.