JAVA: Quick and Safe Class Splitting

When a class is too large and you need to split it apart.. Best way is to utilize an inner-class, which will split A.class into A.class and A$B.class. Move all static methods of the outer class into the inner class.

For example:

class Outer {
  private int var = 0;
  private static void doSomething() { var = 10; /* large remaining chunk of code */ }
}

into:

class Outer {
  private int var = 0;
  private static doSomething() { Inner.doSomething(); }
  static class Inner {
    private static void doSomething() { var = 10; /* large remaining chunk of code */ }
  }
}

NOTES:
1. Yes static inner classes are allowed. And is required in order to have the static inner method.
2. Yes static inner classes can successfully access the outer class' member variables without any tricks.

No comments: