0

Just looking for a confirmation.

public class Indeed{
   public static class Inner implements Runnable{
         public void run()
    {
       System.out.println("Indeed");
    }

  }
    public static void main (String []args)
  {
     Indeed.Inner inner = new Indeed.Inner();
     inner.run();
  }
}

As you can see in the code above, I can declare public void run() without declaring it static. I guess it's implicitly done. Isn't it?

One more question related: Why I cannot use the method run as following: Indeed.Inner.run(); it is static after all, there should not be any need of instantiating the inner member at all? ( I know I am wrong as it does not compile if I do that, however I would like to know why).

Thanks in advance.

Rollerball
  • 12,618
  • 23
  • 92
  • 161

5 Answers5

4

As you can see in the code above, I can declare public void run() without declaring it static. I guess it's implicitly done. Isn't it?

No.

One more question related: Why I cannot use the method run as following: Indeed.Inner.run();

Becuase it's not static.


static class is only valid for inner classes and you can point to a static class by its enclosing class as Indeed.Inner.

This is different from non-static inner class where you need an instance of the enclosing class to create an instance of the same class. For example:

Indeed.Inner inner = new Indeed().new Inner();
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

No, run() is an instance method of the static class Inner. A static (inner) class just makes it possible to use an instance of the class without an enclosing parent instance. When you do Indeed.Inner inner = new Indeed.Inner();, you are creating an instance of the static class, and you are invoking it's run() method on this instance.

NilsH
  • 13,705
  • 4
  • 41
  • 59
1

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Non-static nested classes (inner classes) have access to other members of the enclosing class

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

A static class is just a regular class, in fact more so than a non-static class.

The difference between a static nested class and a top-level class is just access scoping: the static class can access private members of its enclosing class.

Once you get that cleared up, you won't need to ask the question that you are asking here.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Static inner class

public static class Inner implements Runnable

means you can create the instance of them, without having to create the instance of outer class (Indeed)

Indeed.Inner inner = new Indeed.Inner();

Why I cannot use the method run as following: Indeed.Inner.run() ?

the run method is by default not static. To call Indeed.Inner.run() directly, you need to make run() method static too

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • 'static inner class' is a contradiction in terms. ["An inner class is a nested class that is not explicitly or implicitly declared static."](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.3). – user207421 May 29 '13 at 00:25