Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this java .execute() method call mean?

I was doing some reading through the sun java tutorials, and I came across this page here:

How to Make an Applet

Under the heading, "Threads in Applets" I found this piece of code:

   //Background task for loading images.
    SwingWorker worker = (new SwingWorker<ImageIcon[], Object>() {
            public ImageIcon[] doInBackground() {
                final ImageIcon[] innerImgs = new ImageIcon[nimgs];
            ...//Load all the images...
            return imgs;
        }
        public void done() {
            //Remove the "Loading images" label.
            animator.removeAll();
            loopslot = -1;
            try {
                imgs = get();
            } ...//Handle possible exceptions
        }

    }).execute();
}

First up I'm newish, so I'm sorry if this is a stupid question. However I've never heard of that ".excecute()". I don't understand it, and I'm unable to find anything about it from google. I see this here is... an anonymous inner class? (Please correct me) and it is starting a thread to load in images. I thought that the run() method is invoked with a call to start()? Please help me clear this confusion.

like image 586
yoonsi Avatar asked May 25 '12 11:05

yoonsi


People also ask

What does the execute () method do?

Since this is a SELECT statement, the execute( ) method returns a boolean true to indicate that a result set is available. You can then call the Statement object's getResultSet( ) method to retrieve the ResultSet object that contains the data from the database.

What is execute method in Java?

execute : Returns true if the first object that the query returns is a ResultSet object. Use this method if the query could return one or more ResultSet objects. Retrieve the ResultSet objects returned from the query by repeatedly calling Statement.

Can you call a method within a method Java?

Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.

How do you call a method from another method in Java?

We can call a method from another class by just creating an object of that class inside another class. After creating an object, call methods using the object reference variable.


2 Answers

execute is a method of SwingWorker. What you're seeing there is an anonymous class being instantiated and having its execute method called immediately.

I have to admit I'm a bit surprised that code compiles, though, because it seems to be assigning the result of execute to the worker variable, and the documentation tells us that execute is a void function.

If we deconstruct that code a bit, it can be clearer. First, we create an anonymous class extending SwingWorker and create an instance of it, all at the same time (this is the big bit in parentheses):

SwingWorker tmp = new SwingWorker<ImageIcon[], Object>() {
    public ImageIcon[] doInBackground() {
            final ImageIcon[] innerImgs = new ImageIcon[nimgs];
        ...//Load all the images...
        return imgs;
    }
    public void done() {
        //Remove the "Loading images" label.
        animator.removeAll();
        loopslot = -1;
        try {
            imgs = get();
        } ...//Handle possible exceptions
    }

};

Then we call execute and assign the result to worker (which is the bit that, it seems to me, shouldn't compile):

SwingWorker worker = tmp.execute();

Update: And indeed, I tried it and it doesn't compile. So not great example code. This would compile:

SwingWorker worker = new SwingWorker<ImageIcon[], Object>() {
    public ImageIcon[] doInBackground() {
            final ImageIcon[] innerImgs = new ImageIcon[nimgs];
        ...//Load all the images...
        return imgs;
    }
    public void done() {
        //Remove the "Loading images" label.
        animator.removeAll();
        loopslot = -1;
        try {
            imgs = get();
        } ...//Handle possible exceptions
    }

};
worker.execute();
like image 140
T.J. Crowder Avatar answered Sep 19 '22 12:09

T.J. Crowder


The .execute() is calling the execute method on an instance of an anonymous class; i.e. the object created by new SwingWorker<ImageIcon[], Object>(){...}. (It is a class that extends the SwingWorker class.)

According to the javadoc, the execute method schedules the task represented by the instance to be executed on an existing worker thread. The lifecycle of the worker thread (e.g. creation, starting, etcetera) is taken care of by Swing infrastructure.

like image 22
Stephen C Avatar answered Sep 21 '22 12:09

Stephen C