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

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

 public static final java.lang.String INITIAL; 

这条线是由学校提供的,所以我必须假设它是正确的,我无法找到关于常数字段值INITIAL的任何文档,所以如果有人能够提供关于那将是惊人的信息(例如,如何是处理?它存储什么?如果有的话?键入?)。 我在Eclipse中的这一行上得到了一个错误:

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

为什么这个错误在这里? 提前感谢您的评论。

FYI来自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; } } } 

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

最终字段需要分配一次。 你根本就没有分配它。 我们不知道该字段在文档之外的代表意味着什么(“标准初始密码”) – 可能有一些您想要知道的默认密码。 您应该将该值分配给该字段,例如

 public static final String INITIAL = "defaultpassword"; 

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

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

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

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

你可以写:

 return condition; 

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

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

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

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

 public static final java.lang.String INITIAL; 

改成

例如:

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

将变量声明为常量

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

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

final int NUMBER_OF_HOURS_IN_A_DAY = 24;

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

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

final int NUMBER_OF_HOURS_IN_A_DAY = 24; NUMBER_OF_HOURS_IN_A_DAY = 36; 我们将从编译器中获得以下错误:

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

来源和阅读更多

另一个错误

if(test == INITIAL){

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

为什么

equals()比较你要找的内容

==如果引用查看同一位置,则比较引用