Reputation: 481
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;
}
}
}
Upvotes: 14
Views: 91875
Reputation: 6739
You never assign any value to your constant variable at all because 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 a Constant
In declaring variables I showed that it’s easy to assign a value to a int variable:
int numberOfHoursInADay = 24; We know this value is never going to change in the real world so we make sure it doesn’t in the program. This is done by adding the keyword modifier final:
final int NUMBER_OF_HOURS_IN_A_DAY = 24;
In addition to the final keyword you should have noticed that the case of the variable name has changed to be uppercase as per the standard Java naming convention. This makes it far easier to spot which variables are constants in your code.
If we now try and 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 will get the following error from the compiler:
cannot assign a value to final variable NUMBER_OF_HOURS_IN_A_DAY The same goes for any of the other primitive data type variables. To make them into constants just add the final keyword to their declaration.
Another error:
if (test == INITIAL) {
You must use equals()
to compare two strings
why?
equals()
compare againts the content which what you are looking for
==
compare the reference if references are looking at the same location
Upvotes: 0
Reputation: 56
I don't see anywhere in the provided Password.java where the static final INITIAL is being assigned. That must be the problem here.
Upvotes: 0
Reputation: 1502086
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(" ");
}
Upvotes: 31