Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The blank final field INITIAL may not have been initialized

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;

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:

The blank final field INITIAL may not have been initialized

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

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;
    }
}
}
like image 204
Koen Avatar asked Nov 14 '14 17:11

Koen


1 Answers

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";

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.

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

Additionally: any time you have code like this:

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

you can just write:

return condition;

For example, your acceptable method can be written as:

public boolean acceptable(String suggestion) {
    return suggestion.length() >= 6 && !suggestion.contains(" ");
}
like image 169
Jon Skeet Avatar answered Oct 12 '22 12:10

Jon Skeet