1

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?

Community
  • 1
  • 1
bigbluedragon
  • 97
  • 3
  • 9

1 Answers1

2
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:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555