Rahul Raj
Rahul Raj

Reputation: 3459

Able to instantiate static inner class without referring to parent class

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?

Upvotes: 2

Views: 545

Answers (2)

ssc327
ssc327

Reputation: 710

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.

Upvotes: 2

Speakjava
Speakjava

Reputation: 3392

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.

Upvotes: 6

Related Questions