-1

Whenever I compile this, I get an error reading "non-static variable french cannot be referenced from a static context". I have just started working with GUIs, but I cannot find an answer to this. I'm sure it's something simple, but I cannot figure it out!

Any help would be very greatly appreciated.

 public class ReportCard {
 JTextField french = new JTextField ("French Grade") ;
 public static void main (String args[]) {
    JFrame frame = new JFrame ("Report Card") ;
    frame.setSize(400 , 600) ;
    frame.setVisible(true) ;
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

    JPanel bigPanel = new JPanel() ;
    frame.getContentPane().add(bigPanel) ;

    bigPanel.add(french) ;   
  • 1
    possible duplicate of [non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Adrian Leonhard Feb 21 '15 at 02:32

2 Answers2

3

I would strongly suggest reading up on how static works there are many people who can explain it much better than i can.For now you can do a couple of things. make french static

 static JTextField french = new JTextField ("French Grade") ;

or put it in to the main method which will inherently make it static since its already in a static context.

public class ReportCard {
 public static void main (String args[]) {
    JTextField french = new JTextField ("French Grade") ;
    JFrame frame = new JFrame ("Report Card") ;
    frame.setSize(400 , 600) ;
    frame.setVisible(true) ;
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

    JPanel bigPanel = new JPanel() ;
    frame.getContentPane().add(bigPanel) ;

    bigPanel.add(french) ;  
Todoy
  • 1,146
  • 1
  • 9
  • 17
  • 1
    `static`, or "global" variables are a bad idea in normal programming, they are a complete pain in the API within a GUI environment, it's to easy for the reference of the variable to be changed unexpectedly and the whole program to crumble apart. A better solution would be to use the `ReportCard` class's constructor or other methods to build the UI and use the `main` method to get it started... – MadProgrammer Feb 21 '15 at 02:44
1

The variable, french is an instance field of ReportCard, it has no context or meaning outside of an instance of ReportCard, this means that unless you first create an instance of ReportCard, you can't reference the variable.

While you could make french static, this leads you down a dark path of potential issues and is generally a bad idea, it's to easy for the static reference to be changed and your whole program to come apart. static is also NOT a cross object communication mechanism and should never be used as as such.

Instead, you could use ReportCard to create your UI, for example...

public class ReportCard {
 JTextField french = new JTextField ("French Grade") ;
 public static void main (String args[]) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            ReportCard reportCard = new ReportCard();
            reportCard.start();
        }
    }
 }

 public void start() {
    JFrame frame = new JFrame ("Report Card") ;
    frame.setSize(400 , 600) ;
    frame.setVisible(true) ;
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

    JPanel bigPanel = new JPanel() ;
    frame.getContentPane().add(bigPanel) ;

    bigPanel.add(french) ;   
 }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366