when we use final keyword that variable has been declared as constant then what is the necessity of using static ?
I have seen in most of the places that we use
public static final int nVar = 12
for constant data member .
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.
final means that, once assigned the value of variable cannot be modified.
static means “associated with the class”; without it, the variable is associated with each instance of the class. if not, you’ll have one for each instance you create.
staticmeans the variable will remain in memory for as long as the class is loadedThere is no point in declaring a variable like this.
If this is not meant to be modified, why have one copy per instance.
Hence, Class constants need to be declared as
static finalwhere as the variables which you want to be immutable on per instance basis, you declare them asfinal