Background
public static void maxmin(int[] intArray) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for (int i: intArray) { max = (i > max) ? i : max; min = (i < min) ? i : min; // return max and min } }
Then you come up with an briliant idea of creating an Pair inner class.
class Pair { private int a; private int b; public Pair(int a, int b) { this.a = a; this.b = b; } public int getA() { return a; } public int get() { return b; } }
However, it order to use the Pari class in minmax method, you have to qualified new Pair(min, mx) with the new OuterClass(). qualifier. In this case, there is no need for pair to access OuterClass object. To save you some coding, declare the Pair class as static.
Advantages and disadvantages
In addition to removing boiler-plate code, static inner class is very useful for avoiding name collision betwen to class. To refer to a public static inner class, you have to add the OuterClass. prefix.
How does it work?
Let's look into the class file for pair in more detail. Use javap to decompile the OuterClass$Pair.class:
Compiled from "OuterClass.java" class blog.StaticInnerClass.StaticInnerClass$Pair { public blog.StaticInnerClass.StaticInnerClass$Pair(int, int); public int getA(); public int get(); }
As you can see, that static modifier override the default behaviour of adding a final filed for the OuterClass.