简体   繁体   English

声明常量没有值

[英]Declaring constant with no value

If I declare a constant like static final double PI; 如果我声明一个常量,如static final double PI; I'll get The blank final field PI may not have been initialized error. 我会得到The blank final field PI may not have been initialized错误。 Why can't I declare it this way first and initialize it later? 为什么我不能先这样声明并稍后初始化它?

UPDATE : I might not know the initial constant value and have to initialize it after some logic so that I can have a value for it. 更新 :我可能不知道初始常量值,并且必须在一些逻辑之后初始化它,以便我可以为它创建一个值。 That's why I need to initialize it later. 这就是为什么我需要稍后初始化它。

You can initialise it later in the code. 您可以稍后在代码中对其进行初始化。 You just have to initialise it before it can be used. 您必须先将其初始化才能使用它。

It is not write once memory and reads won't block until a value is set. 一旦设置了值,它就不会一次写入内存,并且不会阻塞读取。

Variable is declared as static means, when class is get loaded into memory, all static variables get loaded into the memory as well. 变量被声明为静态方法,当类被加载到内存中时,所有静态变量也被加载到内存中。 On top of that that variable is final means it has to be some value right at the time of class loading. 在该变量之上是final,这意味着在类加载时它必须具有一定的值。 Initialising it in non static block of code means changing its value from nothing(null) to something newly assigned value. 在非静态代码块中初始化它意味着将其值从无(null)更改为新分配的值。

You can see this with the example that even if you didn't initialise a static final variable, you can do it in static block of the class. 您可以通过示例看到这一点,即使您没有初始化静态最终变量,也可以在类的静态块中执行此操作。

class Demo {
       static final String msgHeader;
       /*
           Some lines of code
       */
       static { 
                msgHeader="Please Verify the Input";
       }
 }

Java has to make sure that a final field is only initialized once and never changes. Java必须确保最终字段仅初始化一次并且永远不会更改。 This can only happen during initialization. 这只能在初始化期间发生。

Because constants cannot change once it has been assigned. 因为常量一旦被赋值就不能改变。 :) :)

i guess you have to initialize a first value for that to solve this problem 我想你必须初始化第一个值来解决这个问题

static final double PI=3.14; 静态最终双倍PI = 3.14;

can solve your problem 可以解决你的问题

因为它不会不变,因为您可以在程序的不同位置更改它的值。

Because you are trying to read the variable before it is set. 因为您在设置之前尝试读取变量。

In JAVA you have the provision to set it, anywhere before it is read. 在JAVA中,您可以在读取之前的任何位置进行设置。 But in your case, you have marked it as final and that too, static ,which means 但在你的情况下,你已经将它标记为final ,而且也是static ,这意味着

final : You can set the value only once final :您只能设置一次值
statis : You have made this variable static which means it will be created one per class. statis :您已将此变量设为静态,这意味着每个类将创建一个变量。

Since the field PI is marked as "final" you cannot change it later by assigning new value to the field. 由于字段PI被标记为“最终”,因此以后无法通过为该字段分配新值来对其进行更改。 Final also marks the field PI as constant. 最终还将字段PI标记为常量。 So you need to specify the value during declaration itself which cannot be modified at all . 因此,您需要在声明本身期间指定值,该值根本无法修改。

Like all other normal variables in java, the constant variable also should be declared before it is being used first inside any method. 与java中的所有其他普通变量一样,常量变量也应在任何方法中首先使用之前声明。 If you need to use that variable without initialization, then you should declare that variable as a class variable instead of declaring the variable into the method and this is a constant variable and if you declared this variable as a class variable then you cannot modify its value. 如果你需要在没有初始化的情况下使用该变量,那么你应该将该变量声明为类变量,而不是将变量声明为方法,这是一个常量变量,如果将此变量声明为类变量,则不能修改其值。

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

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