0

My question is why do I get the "non-static variable this cannot be referenced from a static content" error?

I understand that static content can't be used in a non-static context. What is the factor that makes the difference between the first and second block that will cause the error?

The first block gives me the error.

/**FIRST BLOCK**/
package vb503;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author Jairzinho
 */
public class vb503 extends JFrame{
    public static void main(String [] args){
    JFrame frame = new vb503();
    frame.setSize(300,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("vb503");
    frame.setContentPane(new KlikPaneel());// error here
    frame.setVisible(true);
    }
    class KlikPaneel extends JPanel{

    }
}

/** SECOND BLOCK**/

package vb502;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author Jairzinho
 */
public class Vb502 extends JFrame{

    public static void main(String[] args) {
        JFrame frame = new Vb502();
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("vb502");
        **frame.setContentPane(new CijferPaneel());**
        frame.setVisible(true);
    }
}

class CijferPaneel extends JPanel{
    private JLabel label;
    private JTextField tekstvak;
    private double cijfer;

public CijferPaneel(){
    label = new JLabel("voer een cijfer in ");

    tekstvak = new JTextField(10);
    tekstvak.addActionListener(new tekstvakHandler());

    add(label);
    add(tekstvak);
}
class tekstvakHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String invoer = tekstvak.getText();
        cijfer = Double.parseDouble(invoer);

        System.out.println("het ingevoerde cijfer is :" + cijfer);
        if(cijfer >=6){
            System.out.println("dat is een voldoende");

        }
        System.out.println("dank u!");
        System.out.println();
    }
}
}
Jairzinho
  • 11
  • 3
  • 4
    Because you have a non-static inner class. – SLaks Mar 06 '18 at 20:57
  • 4
    "I understand that static content cant be used in a non-static context" No, it's the other way around: non-static content can't be used in a static context. – Andy Turner Mar 06 '18 at 20:57
  • what makes the inner class static ? – Jairzinho Mar 06 '18 at 21:02
  • I don't even get which line exactly gives you the error. Please always mark it, for example using a comment in the code like `// Error here`. – Zabuzard Mar 06 '18 at 21:08
  • @Jairzinho The `static` keyword? – Johannes Kuhn Mar 06 '18 at 21:13
  • thanks for the link it my question is a duplicate yes but i am having trouble understanding the static non-static so i needed someone to explain it like im 5 – Jairzinho Mar 06 '18 at 21:14
  • @Zabuza thats the thing im having problems with i use the class often and to me it seems random when it gives me the error because it happens or doesnt happen the moment i create the instance – Jairzinho Mar 06 '18 at 21:18
  • Please always include the full stacktrace of the error message. It has line information that tells where exactly the error message gets thrown. – Zabuzard Mar 06 '18 at 21:21
  • You could see it like this: static = collective, "belongs" to the class of objects, does not require instance, for example your family name applies to all of your family members. Non static = personal, "belongs" to a certain instance, requires instance, for example your first name belongs to you. Someone else in your family may have the same first name, but is a different person/instance, and it certainly does not apply to everyone in your family. Not the brightest example, but you get the idea. – Morfic Mar 06 '18 at 21:23
  • @zabuza i dont know what a full stacktrace is and i do not find the info on entering code in the question clear on how and what but i appreciate the help – Jairzinho Mar 06 '18 at 21:26
  • @morfic that i understand can u explain it to me in code please – Jairzinho Mar 06 '18 at 21:29
  • what i was doing wrong was i was making the instance of klikPaneel() inside the class Vb503() wich is a static class feel so retarded – Jairzinho Mar 06 '18 at 21:47
  • `Vb503` is not static, however `public static void main` where you indeed do `new new KlikPaneel()` is static. – Morfic Mar 06 '18 at 22:19

0 Answers0