Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of defining an inner class within a static method?

I was reading the book "Head First Java" and at some point it mentioned that an inner class instance must be tied to an outer class instance, which I was already aware of, but with an exception:

A very special case—an inner class defined within a static method. But you might go your entire Java life without ever encountering one of these.

I'm pretty sure that last statement is indeed true, but if the compiler allows it to happen it means that it exists for a reason, otherwise it would be illegal Java. Can someone show me an example of where this would be useful?

like image 348
federico-t Avatar asked Oct 22 '13 19:10

federico-t


People also ask

Can inner class be defined as static?

Inner Classes As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Why would you use an inner class?

Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class.

What is true about a static inner class?

Which statement is true about a static nested class? You must have a reference to an instance of the enclosing class in order to instantiate it.

What is need of creating inner class in Java?

May 25, 2022. An inner class in Java is defined as a class that is declared inside another class. Inner classes are often used to create helper classes, such as views or adapters that are used by the outer class. Inner classes can also be used to create nested data structures, such as a linked list.


2 Answers

It may be special, it may not be.

You're looking at a local class available within a method:

class Foo {
    static void bar(){
       class MyRunnable implements Runnable {
           public void run() {
               System.out.println("No longer anonymous!");
           }    
        };
       Thread baz = new Thread(new MyRunnable());
    }

}

I've seen uses of inner classes that are anonymous like:

class Foo {
    static void bar(){
        Thread baz=new Thread(new Runnable(){
            public void run(){
                System.out.println("quux");
            }
        }
    }
}

This is technically an inner class(though anonymous) and defined in a static method. I personally would create a static nested class that implements Runnable and do:

baz = new Thread(new MyRunnable());

where MyRunnable is defined as:

class Foo {
    static void bar(){
       // SNIP
    }
    static class MyRunnable implements Runnable {
        public void run() {
            System.out.println("No longer anonymous!");
        }    
    }
}
like image 106
nanofarad Avatar answered Sep 20 '22 12:09

nanofarad


Some people take the view that any method that can be static should be static. To such a person, the inner-beauty of the class would not be terribly relevant.

like image 39
bmargulies Avatar answered Sep 19 '22 12:09

bmargulies