views:

99

answers:

2
EventQueue.invokeLater(new Runnable()
  {
     public void run()
        {
           ZipTestFrame frame = new ZipTestFrame();
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setVisible(true);
        }
  });
+9  A: 

That code creates an instance of an anonymous class implementing runnable. That object is passed as an argument to EventQueue.invokeLater, which will invoke it later (meaning it will call the run method on that object at some later point in time).

You don't need a variable to point to the object because you only use that object in that one instance. The method invokeLater does have a local variable pointing to the object (given that the object is passed as an argument and arguments are local variables), which it uses to store the object in the event queue, which now also references the object, until the object is invoked and removed from the queue, at which point it's elligible for garbage collection.

Addendum:

A primitive example of what a simple event queue could look like:

class MyEventQueue {
    Queue<Runnable> queue = new LinkedList<Runnable>();
    public void invokeLater(Runnable r) {
        queue.add(r);
    }
    public boolean hasNext() {
        return !queue.isEmpty();
    }
    public void processNext() {
        queue.poll.run();
    }
}
sepp2k
how will it invoke it later?
Delirium tremens
It is put in a queue i.e. EventQueue and gets executed after the items ahead of it in the queue are processed
A: 

Here's what Event looks like:

class Event { 
    Runnable RunnableObject; 
    public void invokeLater(Runnable runner) { 
        RunnableObject = runner; 
    }
    /* background thread */ 
    public void thread() { 
        while ( condition ) { 
            if ( itsLater ) { 
               RunnableObject->run();
            }
        }
     }
 }

There is an asynchronous method running behind the Event facade that will take the anonymous object you passed to invokeLater and call its run() method. The object still exists, it has a definition - but it has no reference you can use to subsequently interrogate it with, unless you use the run() method to pass the this pointer to something else.

Chris Kaminski