2

The following code executes fine:

public static void main(String [] args) {
        Runnable r = new Runnable() {
            public void run() {
                createGUI();
            }
        } ;

        javax.swing.SwingUtilities.invokeLater(r);
    }

I am curious why the following code will not compile:

 import javax.swing.SwingUtilities;

    public static void main(String [] args) {
                Runnable r = new Runnable() {
                    public void run() {
                        createGUI();
                    }
                } ;

                invokeLater(r);
        }

What is the differenc between javax.swing.SwingUtilities.invokeLater(r); and import javax.swing.SwingUtilities; invokeLater(r);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Buras
  • 3,069
  • 28
  • 79
  • 126
  • 2
    Try changing `import javax.swing.SwingUtilities` to `import static javax.swing.SwingUtilities.*` and your last snippet should work – BackSlash Oct 03 '13 at 22:11

1 Answers1

5

To reference a static member inside a class like that, as a simple name, you need a static import:

import static javax.swing.SwingUtilities.*;

and later you can use

invokeLater(r);

A normal import import javax.swing.SwingUtilities; allows you to refer to the the class SwingUtilities by a simple name, but not any of the members of that class. So with that you could do:

import javax.swing.SwingUtilities;

and

SwingUtilities.invokeLater(r);
rgettman
  • 176,041
  • 30
  • 275
  • 357