If I cannot create an instance of a static class, why I can instantiate a static inner class?
In the code bellow, Counter is a static inner class but it can be instantiated as if it was not:
public class Task {
static class Counter {
int counter = 0;
void increment() {counter++;}
}
public static void main(String[] args) {
Counter counter1 = new Task.Counter();
Counter counter2 = new Task.Counter();
counter1.increment();
counter2.increment();
counter2.increment();
counter2.increment();
System.out.println("counter1: " + counter1.counter);
System.out.println("counter2: " + counter2.counter);
}
}
If Counter was not a static class, it cound be instantiate using the following sintax:
Counter counter1 = new Task().new Counter();
Counter counter2 = new Task().new Counter();
But I cannot figure out the difference between these approaches in practical means.