开发者

non-static method setPrzechowaj(java.lang.String) cannot be referenced froma a static context [duplicate]

This question already has answers here: Closed 10 years ago.

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 setPrzech开发者_开发百科owaj(java.lang.String) cannot be referenced froma a static context any ideas?


Zmienne_pomocnicze.setPrzechowaj(sciezka);

Here you are calling a method directly by classname without instantiating the class. This requires the method to be static, i.e.:

public static void setPrzechowaj (String neew)
{
   n = neew;
}

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.

Zmienne_pomocnicze zp = new Zmienne_pomocnicze();
zp.setPrzechowaj(sciezka);

See also:

  • Understanding Instance and Class Members
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜