简体   繁体   English

为什么要将变量声明为static和final

[英]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; 变量被声明为static以获取其值的最新和单个副本; 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)? 但是为什么同一个变量应该被声明为final ,哪个变量不允许更改为where(常量值)?

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. static这样可以在不创建类实例的情况下访问变量或方法,并且该类只有一个变量,而不是每个实例都有一个变量。

A final class cannot be extended. final堂课不能延期。 A final variable cannot have its value changed, it behaves as a constant. final变量的值不能更改,它表现为常量。 And a final method cannot be over-ridden. 并且final方法不能被覆盖。

The minute a variable is defined as final , it should probably not be referred to as "variable", since it no longer "varies" :) 变量被定义为final那一刻,它可能不应该被称为“变量”,因为它不再“变化”:)

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. static变量不依赖于类的任何特定实例 - 它只与类本身绑定,并且只与范围界定相关联。

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. 所以你是 - 一个staticfinal变量实际上是一个没有绑定到任何特定类的实例并且不会改变的值。 It is a constant value, to be referenced from anywhere in your Java code. 它是一个常量值,可以从Java代码中的任何位置引用。

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. 声明为static的变量意味着它的值由此类的所有实例共享。 Declaring a variable as final gives a slightly better performance and makes your code better readable. 将变量声明为final可以提供稍微好一点的性能,并使您的代码更具可读性。

local variables are on the stack and are not static. 局部variables在堆栈上并且不是静态的。

You can have a static field which may or may not be final. 您可以拥有一个可能是也可能不是最终的静态field You would make the field final if it is not going to change. 如果它不会改变,你会让场地最终。

static final is used in Java to express constants. static final在Java中用于表示常量。 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. Static用于表示类变量,因此不需要为该类实例化对象以访问该变量。

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关键字,则该值不会是常量,因为它可以再次初始化。

static fields can be modified (eg public static fields can be modified by any class). 可以修改static字段(例如,任何类都可以修改public static字段)。 static final fields cannot be modified after initialization. 初始化后无法修改static final字段。

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 ). 静态与获取最新的单个副本无关,除非“单个副本”在这里对于类的所有实例意味着一个和相同的值(但是,我认为你可能会将它与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 ). 当你想要它是一个常量时(实际上就是声明Java常量的方式: 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. 变量应被声明为fields只有当他们requiredusemore than one的类的方法,或者该程序应save their values调用类的方法之间。

for example user.lastName lastName should be field because it is needed during object lifecycle 例如, user.lastName lastName应该是field,因为在object lifecycle needed

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. 变量应被声明为static ,只有当他们正在not要求使用在类的方法不止一种,或者该程序应该not节省话费的类的方法之间的值。

for example Math.max(num1,num2) Im not intristed in num1 and num2 after compleating this operation 例如Math.max(num1,num2)after compleating此操作after compleating Math.max(num1,num2)not intristed num1和num2中

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

You create static final variable to make its value accessible without instantiating an object. 您可以创建static final变量,以便在不实例化对象的情况下访问其值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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