what is the point of having an instance variable as final?
isn´t it better to have that variable set as a static final variable then?
cause if it can’t be changed by any of the objects, then it’s the same as a class (static) variable, right?
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.
Nope.
staticmeans it’s the same across all instances of the class.finalmeans it’s not assignable after its initial assignment. So two instances could have different values for a non-static final variable.There are many reasons you might want to make a variable final; one of the best is clarity. If I read a method and notice that the foo is final, I don’t have to worry about where it’s changing down below – because it isn’t; it can’t. I can make more changes to code with final variables with less concern (“did I change the value of foo before or after bar, and does it matter?”) because I know that some variables aren’t subject to change. It also focuses my attention on the variables that are subject to change – and they’re the ones that deserve more attention.