java - The blank final field INITIAL may not have been initialized -
java - The blank final field INITIAL may not have been initialized -
i'm programming in java. have added comments every method explain they're supposed (according assignment). have added know stub of password.java
(which created after researching javadoc provided school). question not several functions, know there mistakes in testword , setword, i'll handle myself. question line:
public static final java.lang.string initial;
this line provided school, gotta assume correct, cant find documentation anywhere constant field value initial, if provide me info on that amazing (eg. how is handled? store? if anything? type?). im getting error on line in eclipse:
the blank final field initial may not have been initialized
why error here? in advance comments.
fyi code password.java:
package ss.week1; public class password extends java.lang.object { // ------------------ instance variables ---------------- /** * standard initial password. */ public static final java.lang.string initial; // ------------------ constructor ------------------------ /** * constructs password initial word provided in initial. */ public password() { } /** * tests if given string acceptable password. not acceptable: word * less 6 characters or word contains space. * * @param suggestion * @return true if suggestion acceptable */ // ------------------ queries -------------------------- public boolean acceptable(java.lang.string suggestion) { if (suggestion.length() >= 6 && !suggestion.contains(" ")) { homecoming true; } else { homecoming false; } } /** * tests if given word equal current password. * * @param test word should tested * @return true if test equal current password */ public boolean testword(java.lang.string test) { if (test == initial) { homecoming true; } else { homecoming false; } } /** * changes password. * * @param oldpass current password * @param newpass new password * @return true if oldpass equal current password , newpass acceptable password */ public boolean setword(java.lang.string oldpass, java.lang.string newpass) { if (testword(oldpass) && acceptable(newpass)) { homecoming true; } else { homecoming false; } } }
the error compiler says - you've got final field, nil setting it.
final fields need assigned exactly once. you're not assigning @ all. don't know field meant represent beyond documentation ("the standard initial password") - presumably there default password you're meant know. should assign value field, e.g.
public static final string initial = "defaultpassword";
additionally: don't need write java.lang.string
; utilize short name (string
). it's thought utilize fully-qualified names within code; import types you're using, , aware in java.lang
imported automatically.
additionally: don't compare strings using ==
; utilize .equals
instead.
additionally: time have code this:
if (condition) { homecoming true; } else { homecoming false; }
you can write:
return condition;
for example, acceptable
method can written as:
public boolean acceptable(string suggestion) { homecoming suggestion.length() >= 6 && !suggestion.contains(" "); }
java eclipse final
Comments
Post a Comment