I started creating a Hangman game. I want to have a main class, and a method class. I want to get a secret word, but I get an error:
non-static method
getWord()
cannot be referenced from a static context.
Maybe I get this error because no object has been created? What's wrong here and how do I fix this?
PS: maybe implementing it with enum could be better, but I want to start this way.
public class HangmanMain {
public static void main(String[] args) {
String secretWord; /* chosen secret word*/
secretWord = HangmanUtil.getWord();
System.out.println("");
}
}
public class HangmanUtil {
private String[] wordBank = {"pool","ice", "america", "hook", "book", "glass" , "hint", "giraffe"," elephant", "ocean","market"};
String guess;
private int bodyPartsLeft;
String getWord(){
int len = wordBank.length;
int rand = (int)(Math.random() * (len + 1));
return wordBank[rand];
}
}