1
package test2;

public class NewJFrame extends javax.swing.JFrame {

    private static void valueGen() {
        String x = jTextField1.getText();
        System.out.println(x);
    }

    public NewJFrame() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        jTextField1.setText("Hello");
    }

    public static void main(String args[]) {
        valueGen();

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
}

I have a program as shown above.

I need to access the value of jTextField1 from the function valueGen().
But I get the error:

non-static variable cannot be referenced from static context

What should I do in order to access the value of jTextField1 from valueGen()?

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
Sajith
  • 113
  • 2
  • 11

2 Answers2

2

Well, it depends on what you need.

If you only have one jTextField1 in your entire program (I doubt that's the case, though), simply make it static:

private static javax.swing.JTextField jTextField1;

If however, you have the more likely scenario of having multiple NewJFrame objects, then add a getter method to your code:

public javax.swing.JTextField getTextField() {
    return this.jTextField1;
}

And then add it as a parameter to valueGen:

private static void valueGen(javax.swing.JTextField jTextField1) {
  String x = jTextField1.getText();
  System.out.println(x);
}

Now, when you call valueGen simply use the getter method:

NewJFrame myFrame = NewJFrame();
//some code
valueGen(myFrame.getTextField());

That way you can still have a static method which can handle any of the object instances' text field.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
0

From your question it appears you are not aware of what the role of the static qualifier is, so Il focus on explaining that.

When you define a class and add non-static member variables, you're defining how each instance of that class will look like. So, for example, if you define

class A {
    public int x;
}

and somewhere you do:

A a1 = new A ();
A a2 = new A ();
a1.x = 1;
a2.x = 2;
System.out.println (a1.x+", "+a2.x);

The output will be 1, 2. This is the case because a1 and a2 point to two separate objects, each one with its own x.

However, when you declare a variable as static there is a single instance of such variable shared by all instances of that class. In fact the variable is considered as belonging to the class itself, rather than an instance. A static variable can be referenced without any object instance, just using the name of the class (A.x).

Similarly to how a class can have variables of its own, it can have methods of its own (static methods). A static method can be invoked using the class name (A.m1()). The method can refer to static variables, but not to non-static ones. The reason is that there might be multiple instances of the class, each with a different value for the variable, so which one should be used?

So, from static methods you can refer to static variables, while from non-static ones you can refer both to static and non-static variables. In your example you have a static method trying to access a non static variable, and that's not allowed. I suspect in your case you will have a single instance of the Jframe subclass, in which case you could make the method static or make the variable non-static and obtain the same result, but in genenral it's better to avoid static variables when possible.

Roberto Attias
  • 1,883
  • 1
  • 11
  • 21