Question

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);

Was it helpful?

Solution

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top