Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this block of code mean?

Tags:

java

What does the second block below run() in the anonymous class new Runnable() { that has no identifier or declaration preceding it mean:

        public BackgroundThread(final Runnable runnable)
        {
            super(new Runnable() {

                final Runnable val$runnable;

                public void run()
                {
                    Process.setThreadPriority(10);
                    runnable.run();
                }


                {
                    runnable = runnable1;
                    super();
                }
            });
        }

Edit: yes it is decompiled code.

like image 704
Raj Avatar asked Feb 06 '12 11:02

Raj


2 Answers

It's an instance initializer - called as part of the constructor. In an anonymous inner class, you can't explicitly declare a constructor, so instance initializers are sometimes used instead. In this case it's pretty pointless, as the run method could just use runnable directly - it would still be captured at the same time.

(This code doesn't look like it's complete or valid, actually - given that the instance initializer mentions runnable1 which doesn't appear anywhere else. I'd also not expect the instance initializer to include a super() call. Is this possibly decompiled code?)

like image 197
Jon Skeet Avatar answered Oct 04 '22 12:10

Jon Skeet


It's an initialization block. It gets compiled into every constructor.

See "Initializing Instance Members" in the tutorial.

like image 25
NPE Avatar answered Oct 04 '22 13:10

NPE