Java 空白的最终字段 INITIAL 可能尚未初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26935453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:31:54  来源:igfitidea点击:

The blank final field INITIAL may not have been initialized

javaeclipsefinal

提问by Koen

I'm programming in Java. I have added comments to every method to explain what they're supposed to do (according to the assignment). I have added what I know to the stub of Password.java(which I created after researching a javadoc provided by the school). My question is not about several functions, I know there are mistakes in testWord and setWord, but I'll handle that myself. My question is about this line:

我正在用 Java 编程。我已经为每种方法添加了注释来解释它们应该做什么(根据作业)。我已将我所知道的添加到Password.java(我在研究了学校提供的 javadoc 后创建的)的存根中。我的问题不是关于几个函数,我知道 testWord 和 setWord 中存在错误,但我会自己处理。我的问题是关于这一行:

public static final java.lang.String INITIAL;

This line is provided by the school, so I gotta assume that it is correct, I cant find any documentation anywhere about the constant field value INITIAL, so if anyone could provide me with info on that that would be amazing (eg. how is is handled? what does it store? if anything? type?). Also Im getting an error on this line in Eclipse:

这条线是由学校提供的,所以我必须假设它是正确的,我在任何地方都找不到关于常量字段值 INITIAL 的任何文档,所以如果有人能向我提供这方面的信息,那将是惊人的(例如。处理?它存储什么?如果有的话?类型?)。我也在 E​​clipse 中的这一行上收到错误:

The blank final field INITIAL may not have been initialized

空白的最终字段 INITIAL 可能尚未初始化

Why is this error here? Thanks in advance about the comments.

为什么这里会出现这个错误?提前感谢评论。

FYI the code from Password.java:

仅供参考 Password.java 中的代码:

package ss.week1;

public class Password extends java.lang.Object {

// ------------------ Instance variables ----------------

/**
 * The standard initial password.
 */

public static final java.lang.String INITIAL;

// ------------------ Constructor ------------------------

/**
 * Constructs a Password with the initial word provided in INITIAL.
 */

public Password() {

}

/**
 * Tests if a given string is an acceptable password. Not acceptable: A word
 * with less than 6 characters or a word that contains a space.
 * 
 * @param suggestion
 * @return true If suggestion is acceptable
 */

// ------------------ Queries --------------------------

public boolean acceptable(java.lang.String suggestion) {
    if (suggestion.length() >= 6 && !suggestion.contains(" ")) {
        return true;
    } else {
        return false;
    }
}

/**
 * Tests if a given word is equal to the current password.
 * 
 * @param test Word that should be tested
 * @return true If test is equal to the current password
 */

public boolean testWord(java.lang.String test) {
    if (test == INITIAL) {
        return true;
    } else {
        return false;
    }
}

/**
 * Changes this password.
 * 
 * @param oldpass The current password
 * @param newpass The new password
 * @return true if oldpass is equal to the current password and that newpass is an acceptable password
 */

public boolean setWord(java.lang.String oldpass, java.lang.String newpass) {
    if (testWord(oldpass) && acceptable(newpass)) {
        return true;
    } else {
        return false;
    }
}
}

采纳答案by Jon Skeet

The error is exactly what the compiler says it is - you've got a final field, but nothing setting it.

错误正是编译器所说的 - 你有一个 final 字段,但没有设置它。

Final fields need to be assigned to exactlyonce. You're not assigning to it at all. We don't know what the field is meant to represent beyond the documentation ("The standard initial password") - presumably there is some default password which you're meant to know. You should assign that value to the field, e.g.

最后的字段需要被分配给恰好一次。你根本没有分配给它。我们不知道该字段在文档之外代表什么(“标准初始密码”) - 大概有一些您应该知道的默认密码。您应该将该值分配给该字段,例如

public static final String INITIAL = "defaultpassword";

Additionally: you don't need to write java.lang.String; just use the short name (String). It's very rarely a good idea to use fully-qualified names within your code; just import the types you're using, and be aware that everything in java.langis imported automatically.

另外:你不需要写java.lang.String; 只需使用简称 ( String)。在代码中使用完全限定的名称很少是一个好主意;只需导入您正在使用的类型,并注意其中的所有内容java.lang都是自动导入的。

Additionally: don't compare strings using ==; use .equalsinstead.

另外:不要使用==;比较字符串 使用.equals代替

Additionally: any time you have code like this:

另外:任何时候你有这样的代码:

if (condition) {
    return true;
} else {
    return false;
}

you can just write:

你可以写:

return condition;

For example, your acceptablemethod can be written as:

例如,您的acceptable方法可以写为:

public boolean acceptable(String suggestion) {
    return suggestion.length() >= 6 && !suggestion.contains(" ");
}

回答by Kick Buttowski

You never assign any value to your constant variable at all because constant variable needs to be assigned once

您根本不会为常量变量分配任何值,因为常量变量需要分配一次

public static final java.lang.String INITIAL;

change to

改成

For example:

例如:

public static final java.lang.String INITIAL ="initial";

Declaring a Variable as a Constant

将变量声明为常量

In declaring variables I showed that it's easy to assign a value to a int variable:

int numberOfHoursInADay = 24; We know this value is never going tochange in the real world so we make sure it doesn't in the program.This is done by adding the keyword modifier final:

final int NUMBER_OF_HOURS_IN_A_DAY = 24;

In addition to the final keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention. This makes it far easier to spot which variables are constants in your code.

If we now try and change the value of NUMBER_OF_HOURS_IN_A_DAY:

final int NUMBER_OF_HOURS_IN_A_DAY = 24; NUMBER_OF_HOURS_IN_A_DAY = 36; we will get the following error from the compiler:

cannot assign a value to final variable NUMBER_OF_HOURS_IN_A_DAY The same goes for any of the other primitive data type variables. To make them into constants just add the final keyword to their declaration.

在声明变量时,我展示了为 int 变量赋值很容易:

int numberOfHoursInADay = 24; 我们知道这个值在现实世界中永远不会改变,所以我们确保它不会出现在程序中。这是通过添加关键字修饰符 final 来完成的:

最终整数 NUMBER_OF_HOURS_IN_A_DAY = 24;

除了 final 关键字之外,您应该已经注意到,根据标准 Java 命名约定,变量名的大小写已更改为大写。这使得更容易发现代码中哪些变量是常量。

如果我们现在尝试更改 NUMBER_OF_HOURS_IN_A_DAY 的值:

最终整数 NUMBER_OF_HOURS_IN_A_DAY = 24; NUMBER_OF_HOURS_IN_A_DAY = 36;我们将从编译器中得到以下错误:

无法为最终变量 NUMBER_OF_HOURS_IN_A_DAY 赋值 任何其他原始数据类型变量也是如此。要将它们变成常量,只需将 final 关键字添加到它们的声明中。

Source and Read More

来源和阅读更多

Another error:

另一个错误

if (test == INITIAL) {

如果(测试==初始){

You must use equals()to comparetwo strings

您必须使用equals()比较两个字符串

why?

为什么

equals()compare againts the content which what you are looking for

equals()比较您要查找的内容

==compare the reference if references are looking at the same location

==如果引用正在查看相同的位置,则比较引用

回答by glaze

I don't see anywhere in the provided Password.java where the static final INITIAL is being assigned. That must be the problem here.

我在提供的 Password.java 中没有看到分配静态最终 INITIAL 的任何地方。那一定是这里的问题。