This is my program so far:

package allin1;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Allin1 {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton b1 = new JButton("Exit");
    public static void main(String[] args) {
        panel.add(b1); //THIS IS THE PROBLEM??
    }
}

The error I am getting from this is, "Non-static variable panel, cannot be referenced from a static context". I am not exactly clear what to do about this to be honest. I am fairly new to Java, and the whole static, public thing is beyond me truly.... Please help me with this!!

The error I am getting from this is, "Non-static variable panel, cannot be referenced from a static context".

Let's analyze what the compiler is trying to tell us:

  • Non-static variable panel, let's see... variable panel is declared on line 7, and there's no static keyword there, so it is non-static.

  • cannot be referenced, this means: "you cannot use it".

  • from a static context, the "static context" the compiler is talking about is (in this case) your main method (starting at line 9). Your main method is a static context because it is a static method (the static keyword is in its signature, you can check yourself: line 9: public static void main(String[] args))

To conclude: you cannot use variable panel in your main method because main is static and your variable is not.

There's a good reason for why this isn't possible (you can skip to the end of the post if you're not interested in the reason):
First we have to differentiate between class variables and instance variables.
A good explanation is given here, but it boils down to class variable => static, instance variable => non-static.

-- From here I assume that you've read the page I linked to. --

public class A
{
    private static int a; // class variable
    private long b; // instance variable
    private double c; // instance variable
    private static boolean d; // class variable

    // compiles
    public static void aStaticMethodOperatingOnAClassVariable()
    {
        // There's only one class variable 'a', there can never be confusion
        // as to which 'a' you are referring. Here 'a' is referred to from a
        // static context.
        System.out.println(a);
    }

    // compiles
    public void aNonStaticMethodOperatingOnAClassVariable()
    {
        // There's only one class variable 'a', there can never be confusion
        // as to which 'a' you are referring. Here 'a' is referred to from a
        // non-static context.
        System.out.println(a);
    }

    // does NOT compile (this is what YOU are facing)
    public static void aStaticMethodOperatingOnAnInstanceVariable()
    {
        // Per instance of this class, there's an instance variable b.
        // This means: there could be multiple instance variables b.
        // Since a static method does not operate on a class instance (object)
        // there's no way to figure out which 'b' you are referring to here.
        // E.g. for class A:
        // Class variables: a, d (shared across all instances of the class)
        // 
        // A x = new A();
        // A y = new A();
        // Both x and y are instances of class A and therefore both can access
        // the class variables a and d because these are shared across all instances.
        // And both x and y can also access their own instance variables c and d.
        // That is: if x refers to its instance variable c, it can never be the c
        // instance variable of y and vice versa.
        System.out.println(b);
    }

    // compiles
    public void aNonStaticMethodOperatingOnAnInstanceVariable()
    {
        // This is a non-static method, therefore it can access instance variables.
        // There's only one instance variable per class instance. So it is clear
        // which 'b' you are referring to here. (In total there are multiple, but
        // from this non-static context there's only one).
        System.out.println(b);
    }
}

How to resolve the error?
1. Make panel static;
2. Move the declaration of panel to your main method;

Edited 3 Years Ago by mvmalderen

Your main method is static, so you can only access static variable inside it.
And you have not decalred JPanel object as a static,

So as tux4life says:

Either you have to declare JPanel as static or
You have to move that decalration inside main method. or
you can create constructor of that class and put it all inside this.

As per your convienient way, you can solve this problem.

Also mark this thread as solved, if you have already solved your problem.

Thanks

Comments
Just repeats previous post's info

The way I have been taught is to include a constructor method which initates any variables when the object is invoked:

package allin1;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Allin1 {
    JFrame frame;
    JPanel panel;
    JButton b1;

    /**
     * This is our constructor
     * This method is executed as the object is invoked
     * In here we can include all our declarations
     */
    public Allin1(){
        frame = new JFrame("Title");
        panel = new JPanel();
        b1 = new JButton("Exit");

        panel.add(b1);
    }

    public static void main(String[] args) {
        new Allin1();
    }
}

Hope this helps. If anything needs explaining then just send me a PM

If anything needs explaining then just send me a PM

If anything needs explaining he better replies with a post, which - on a forum - is a far more superior way to get help than by a PM.

Edited 3 Years Ago by mvmalderen

This article has been dead for over six months. Start a new discussion instead.