I'm not a java guy but I've inherited some code I need to patch up. I pulled the source into netbeans and I'm getting the error: Anonymous class implements interface; cannot have arguments.
Here's the code:
Executor background = Executors.newSingleThreadExecutor();
Runnable mylookupThread = new Runnable(FilePath, SearchIndex)
{
public void run()
{ MainWindow.this.processFile(this.val$FilePath);
Thread t = new Thread(new lookupThread(MainWindow.arrFile, true, false, this.val$SearchIndex));
t.setName("Lookup");
t.setPriority(10);
t.start();
}
};
background.execute(mylookupThread);
Executor statusThread = Executors.newSingleThreadExecutor();
Runnable myStatusThread = new Runnable()
{
public void run()
{ MainWindow.this.updateStatus();
}
};
statusThread.execute(myStatusThread);
The error pops up on the second line. Help?!?
An anonymous class of the form new -interface- implicitly extends Object. You have to use one of the constructors for Object. There is only one - the no-args constructor.
This is the problem line (as you said:)
What's happening is that we're defining a class on-the-fly, and that class implements the
Runnable
interface. When you use this syntax, the items in the parentheses are intended as constructor arguments for the superclass. SinceRunnable
is an interface, not a class, it has no constructors at all, so there are definitely none that take arguments.That said, whatever those are supposed to be, they're not used in the body of the anonymous class, so to a first approximation, you want to just drop what's inside the parentheses altogether.
@Victor is right that you can create another class. You can also use variables inside an anonymous class that are
final
. Something like the following will work.Btw. It's a little strange to create a thread inside the
Runnable
of a thread executing in an executor. Not sure why you wouldn't just spawn theLookupThread
directly and remove the anonymous class altogether.Make
mylookupThread
separate class, make it's instance and pass it toExecutor
:Other way around is to make
filePath, searchIndex
final: