8

I was reading the book "Head First Java" and at some point it mentioned that an inner class instance must be tied to an outer class instance, which I was already aware of, but with an exception:

A very special case—an inner class defined within a static method. But you might go your entire Java life without ever encountering one of these.

I'm pretty sure that last statement is indeed true, but if the compiler allows it to happen it means that it exists for a reason, otherwise it would be illegal Java. Can someone show me an example of where this would be useful?

federico-t
  • 12,014
  • 19
  • 67
  • 111

4 Answers4

3

It may be special, it may not be.

You're looking at a local class available within a method:

class Foo {
    static void bar(){
       class MyRunnable implements Runnable {
           public void run() {
               System.out.println("No longer anonymous!");
           }    
        };
       Thread baz = new Thread(new MyRunnable());
    }

}

I've seen uses of inner classes that are anonymous like:

class Foo {
    static void bar(){
        Thread baz=new Thread(new Runnable(){
            public void run(){
                System.out.println("quux");
            }
        }
    }
}

This is technically an inner class(though anonymous) and defined in a static method. I personally would create a static nested class that implements Runnable and do:

baz = new Thread(new MyRunnable());

where MyRunnable is defined as:

class Foo {
    static void bar(){
       // SNIP
    }
    static class MyRunnable implements Runnable {
        public void run() {
            System.out.println("No longer anonymous!");
        }    
    }
}
nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

Some people take the view that any method that can be static should be static. To such a person, the inner-beauty of the class would not be terribly relevant.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
1

Here's a fabricated example of an inner class within a static method. It can be claimed that

  1. It does not need to be declared outside the static method as it is not required elsewhere
  2. It should be a named class (i.e. not anonymous) as it is being used multiple times

    class Race {
        public static void main(String[] args) throws Exception{
            class Runner implements Runnable {
                final String name;
                long time = -1;
                Runner(String name) { this.name = name; }
                public void run() {
                    try {
                        long start = System.currentTimeMillis();
                        time = -2;
                        System.out.printf("Start %s\n", name);
                        for (int i = 0; i < 10; i++) {
                            Thread.sleep(1000);
                        }
                        System.out.printf("End %s\n", name);
                        this.time = System.currentTimeMillis() - start;
                    } catch (InterruptedException e) {
                        time = -3;
                    }
                }
                long time() { return time; }
            }                
            Runner r1 = new Runner("One");
            Runner r2 = new Runner("Two");
            Thread one = new Thread(r1);
            Thread two = new Thread(r2);
            one.start();
            two.start();
            one.join();
            two.join();
            System.out.printf("One: %s, Two: %s\n", r1.time(), r2.time());
            System.out.printf("%s wins\n", r1.time() < r2.time() ? "one" : "two");
        }
    }
    
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
0

I don't know the full context, but closures (i.e. Guava's Function implementation) and implementations defined in an utility class could be an example.

However, after searching for a while, I haven't found anonymous closure examples in Guava itself.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68