An empty final INITIAL field may not have been initialized

I am programming in Java. I added comments to each method to explain what they should do (according to assignment). I added what I know to the Password.java stub (which I created after the javadoc study provided by the school). My question is not about several functions, I know that there are errors in testWord and setWord, but I will handle it myself. My question is about this line:

 public static final java.lang.String INITIAL; 

This line is provided by the school, so I have to assume that it is correct, I cannot find any documentation anywhere about the constant value of the INITIAL field, so if anyone could provide me with information about what it would be surprisingly (e.g. how is this handled? What does it store? if anything? type?). Also Im getting an error on this line in Eclipse:

An empty final INITIAL field may not have been initialized

Why is this error here? Thanks in advance for your comments.

FYI 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; } } } 
+7
java eclipse final
source share
3 answers

The error is exactly what the compiler says - you have the last field, but nothing is set.

End fields must be assigned exactly once. You do not appoint him at all. We do not know which field is intended for presentation outside the documentation ("Standard initial password") - presumably there is some default password that you should know. You must assign this value to a field, for example.

 public static final String INITIAL = "defaultpassword"; 

Optional: you do not need to write java.lang.String ; just use a short name ( String ). It is very rarely recommended that you use fully qualified names in your code; just import the types you use and keep in mind that everything in java.lang imported automatically.

Optional: do not compare strings with == ; use .equals instead .

Additionally: every time you have a code like this:

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

you can simply write:

 return condition; 

For example, your acceptable method might be written as:

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

You never assign any value to your constant variable at all, since the 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 constants

In the variable declaration, I showed that it is easy to assign the value to int variable:

int numberOfHoursInADay = 24; We know that this value will never be in the real world, so we are sure that it does not work in the program. This is done by adding the final keyword modifier:

final int NUMBER_OF_HOURS_IN_A_DAY = 24;

In addition to the last keyword, you should notice that the case of the variable name has changed to uppercase according to the Java Naming Convention standard. This greatly simplifies the definition of variables as constants in your code.

If we try to 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 get the following error from the compiler:

cannot assign value to final variable NUMBER_OF_HOURS_IN_A_DAY. the same applies to any of the other primitive data type variables. Doing them in constants simply adds the final keyword to their declaration.

Source and Read more

Another mistake :

if (test == INITIAL) {

You should use equals() to compare two lines

why

equals() compare the content you are looking for

== compare the link if the links are looking at the same location

0
source share

I see nowhere in the provided Password.java where a static final INITIAL is assigned. That must be the problem here.

0
source share

All Articles