简体   繁体   中英

Why should a variable be declared as static and final

A variable is declared as static to get the latest and single copy of its value; it means the value is going to be changed somewhere. But why should the same variable be declared as final , which will not allow the variable to be changed else where (constant value)?

static so that the variable or method can be accessed without creating a class instance, and there is only one variable for the class instead of one for each instance.

A final class cannot be extended. A final variable cannot have its value changed, it behaves as a constant. And a final method cannot be over-ridden.

The minute a variable is defined as final , it should probably not be referred to as "variable", since it no longer "varies" :)

A static variable is not tied to any particular instance of a class -- it is only tied to the class itself and only from a scoping standpoint.

So there you are -- a static and final variable is actually a value that is not tied to any particular instance of class and does not vary. It is a constant value, to be referenced from anywhere in your Java code.

At some point, when you should decide to change the value of this constant, it only takes one change to propagate this change correctly to all other classes that use this constant.

A variable declared as static means that its value is shared by all instances of this class. Declaring a variable as final gives a slightly better performance and makes your code better readable.

local variables are on the stack and are not static.

You can have a static field which may or may not be final. You would make the field final if it is not going to change.

static final is used in Java to express constants. Static is used to express class variables, so that there is no need to instantiate an object for that class in order to access that variable.

Final methods can't be overriden and final variables can only be initialised once.

If you only use the static keyword, that value will not be a constant as it can be initialised again.

static fields can be modified (eg public static fields can be modified by any class). static final fields cannot be modified after initialization.

Static has nothing to do with getting the latest and single copy unless "single copy" here means one and the same value for all the instances of a class (however, I think you may be confusing it with volatile ). Static means class variable. You make it final when you want that to be a constant (that's actually the way Java constants are declared: static final ).

可能是提供类似于constants东西。

Like you mention yourself, this is done to create constants. You create a single field to hold a value with a specific meaning. This way you don't have to declare that value everywhere, but instead you can reference the static.

Variables should be declared as fields only if they're required for use in more than one method of the class or if the program should save their values between calls to the class's methods.

for example user.lastName lastName should be field because it is needed during object lifecycle

Variables should be declared as static only if they're not required for use in more than one method of the class or if the program should not save their values between calls to the class's methods.

for example Math.max(num1,num2) Im not intristed in num1 and num2 after compleating this operation

Final会停止从中继承的任何类

You create static final variable to make its value accessible without instantiating an object. EG:

public class MyClass
{
     public static final String endpoint= "http://localhost:8080/myClass":
     /* ...*/
}

Then you can access to the data using this line:

MyClass.endpoint

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM