Possible Duplicate:
non static method cannot be referenced from static context
hey i have problem with JDialogForm. I have created it using netbeans 6.8. That JDialogForm have textfield and button below it. and here is some code…
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String sciezka = jTextField1.getText();
if (sciezka.length() > 0)
{
Zmienne_pomocnicze.setPrzechowaj(sciezka);
}
}
Now i want to copy that string “sciezka” to my main window but if I do it like this
public class Zmienne_pomocnicze {
public String n;
public void setPrzechowaj (String neew)
{
n = neew;
}
public String getPrzechowaj ()
{
return n;
}
}
i get error in jButton1: non-static method setPrzechowaj(java.lang.String) cannot be referenced froma a static context
any ideas?
Here you are calling a method directly by classname without instantiating the class. This requires the method to be
static, i.e.:But you usually just want to create a reuseable instance of the class and call the method on it so that this variable/behaviour doesn’t get shared/applied among all existing instances of the class.
See also: