空白的最终字段INITIAL可能尚未初始化 [英] The blank final field INITIAL may not have been initialized

查看:816
本文介绍了空白的最终字段INITIAL可能尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

public static final java.lang.String INITIAL;

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

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可能尚未初始化

The blank final field INITIAL may not have been initialized

为什么会出现此错误?
提前感谢您的评论。

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

FYI来自Password.java的代码:

FYI the code from 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;
    }
}
}


推荐答案

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

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

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

Final fields need to be assigned to exactly once. 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";

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

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.lang is imported automatically.

另外:不要使用 == ;使用 .equals 代替

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

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

Additionally: any time you have code like this:

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

你可以写:

return condition;

例如,您的可接受的方法可以是写为:

For example, your acceptable method can be written as:

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

这篇关于空白的最终字段INITIAL可能尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆