2

Before I start, I would like to point out that this post is not a duplicate of:

How to instantiate non static inner class within a static method
or

Why instantiation of static nested class object is allowed?

In fact I would like to hear why it behaving strangely in my sample code. As of my understanding, we can instantiate a static inner class something like this:

new OuterClass.InnerClass();

But in my sample code, I'm able to instantiate static inner class something like:

new InnerClass();

I will put my sample code here:

import java.util.*;

class GFG {

static class ListComparator implements Comparator<Integer>{

            @Override
            public int compare(Integer o1, Integer o2){
                if(o1>o2){
                    return -1;
                }
                else if(o1<o2){
                    return 1;
                }
                else
                    return 0;
            }
}

public static void main (String[] args) {
    //code
    List<Integer> list = new ArrayList<Integer>();
    list.add(3);
    list.add(1);
    list.add(2);
    list.add(4);
    Collections.sort(list,new ListComparator());    
    for(Integer i : list){
        System.out.println(i);
    }

 }
}

The above code run without any error and I get output in descending order. Can anyone explain how I am able to directly instantiate a static inner class like this: new ListComparator();

I was expecting error in this case, but surprised by the output. I know my static inner class implementing Comparator interface, but I was wondering making it static would raise a conflict with the behavior of static class. But surprisingly did not happen and I'm curious why it didnt happen?

Rahul Raj
  • 3,197
  • 5
  • 35
  • 55
  • 2
    Why are you expecting an error? ListComparator is in scope, so there's no need to qualify it. It's much like accessing a static variable or method. – Bernhard Barker Aug 22 '17 at 10:27
  • @Dukeling Thanks for the response. Actually I wanted to know how I'm able to instantiate especially if thats static class? How its possible to instantiate a static class? ListComparator is a static inner class here. – Rahul Raj Aug 22 '17 at 10:30
  • "how I'm able to instantiate especially if thats static class" - wouldn't that just make this a duplicate of [this post you linked](https://stackoverflow.com/questions/12836506/why-instantiation-of-static-nested-class-object-is-allowed)? – Bernhard Barker Aug 22 '17 at 10:32
  • Alright, so you mean to say a static inner class gets treated much like a method. Am I right? – Rahul Raj Aug 22 '17 at 10:34
  • 1
    In terms of scope, yes. – Bernhard Barker Aug 22 '17 at 10:35

2 Answers2

6

The answer is simply that the static inner class is in the same scope as the main method. As such, it is not necessary to explicitly reference it from GFG (although you can if you want).

Think of it like this. If you had another static method in GFG, like static void foo() you wouldn't need to call that as GFG.foo() in the main method. It's the same thing for the static inner class.

Speakjava
  • 3,177
  • 13
  • 15
2

When you make a static inner class, you are saying that the inner class does not require a reference to its enclosing class. Making it static does not prohibit instantiation.

For more details check out item 22 of Effective Java by Joshua Bloch. Here is an excerpt:

A static member class is the simplest kind of nested class. It is best thought of as an ordinary class that happens to be declared inside another class and has access to all of the enclosing class’s members, even those declared private. A static member class is a static member of its enclosing class and obeys the same accessibility rules as other static members. If it is declared private, it is accessible only within the enclosing class, and so forth.

ssc327
  • 690
  • 1
  • 9
  • 19