What is the purpose of defining an inner class in a static method?

I read the book " Head First Java ", and at some point he mentioned that the inner instance of the class should be bound to an instance of the outer class, which I was already familiar with, but with the exception:

A very special case is the inner class defined in the static method . But you can go through your entire Java life without ever encountering one of this.

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

+7
java oop static inner-classes
source share
4 answers

It may be special, it may not be.

You are viewing a local class that is available as part of the 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 saw the use of inner classes anonymous as:

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

This is technically an inner class (albeit anonymous) and is defined by the static method. I personally would create a static nested class that implements Runnable and does:

 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!"); } } } 
+3
source share

Some people believe that any method that can be static should be static. For such a person, the inner beauty of this class would not be terribly relevant.

+1
source share

Here is an example of an inner class in a static method. It can be argued that

  • It should not be declared outside the static method, since it is not required elsewhere
  • It must be a named class (i.e. not anonymous), as it is used several 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"); } } 
+1
source share

I do not know the full context, but closures (i.e. the implementation of the Guava Function ) and implementations defined in the utility class can be an example.

However, after some searching, I did not find anonymous closing examples in Guava itself.

0
source share

All Articles