I have written the following code, but continually get a ‘non-static method getText() can not be referenced from a static context’ error.
Could someone help get me on the right track here?
public class ISBNText extends JTextField
{
protected static String bookNum;
protected JTextField bookText;
public ISBNText()
{
super(20);
bookText = new JTextField();
}
public String getISBN()
{
String bookNum = ISBNText.getText();
return bookNum;
}
private String validateISBN(String bookNum)
}
This line:
should just be:
which is implicitly:
The call
ISBNText.getText()is trying to call it as if it’s a static method – i.e. associated with the type rather than with any specific instance of the type. That clearly doesn’t make sense, as the text is associated with an instance of the type. The two alternatives I’ve shown you are equivalent, finding the text of theISBNTextthatgetISBNhas been called on.