문제

So I've come accross this bit of code while scavaging through org.eclipse.ui.internal.Workbench:

        final boolean[] initOK = new boolean[1];

        if (getSplash() != null) {

            final boolean[] initDone = new boolean[] { false };
            final Throwable[] error = new Throwable[1];
            Thread initThread = new Thread() {
                /*
                 * (non-Javadoc)
                 * 
                 * @see java.lang.Thread#run()
                 */
                public void run() {
                    try {
                        // declare us to be a startup thread so that our
                        // syncs will be executed
                        UISynchronizer.startupThread.set(Boolean.TRUE);
                        initOK[0] = Workbench.this.init();
                    } catch (Throwable e) {
                        error[0] = e;
                    } finally {
                        initDone[0] = true;
                        display.wake();
                    }
                }
            };
            initThread.start();

          ..............
       }

What's the purpose of creating arrays with just one element? Is it faster? I'm definitly missing something here.

도움이 되었습니까?

해결책

The advantage of using this approach is that the arrays cannot be modified (because they are final), however, their content can be changed.

A useful example is that you can use these arrays and modify their first entry in anonymous inner classes without having to make them (static) fields.


As stated in the comments above, another use-case is that this array can be used to pass it to a method expecting an array type.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top