7

Possible Duplicate:
Why cant we have static method in an inner class?

Hi all, in java what exactly is the reason that we cannot declare static methods in [public] inner classes unless those inner classes are also declared static?

Amazingly top level classes can have any number of static methods without the need to have any special modifier

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634

2 Answers2

2

Non-static inner classes come into existence only in the context of an instance of the outer class.

So ... if you're going to have a static method, the whole inner class has to be static. Without doing that, you couldn't guarantee that the inner class existed when you attempted to call the static method.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • 8
    This is just flat-out wrong. It's the *instance* of the inner class that exists in the context of the outer, and that only be virtue of having an implicit and hidden reference to the outer class. That has nothing to do with the class definition. – Lawrence Dol May 10 '11 at 05:25
1

The question to ask is -- if you do have a static method inside an inner class, how would you call that static method? The answer is, you can't.

An inner class is tied to instances of the outer class.

From Effective Java -- "Each instance of a nonstatic [nested] class is implicitly associated with an enclosing instance of its containing class".

This is the reason for making the "inner" class static. This actually a static nested class and its a full-blown class thats merely present in the enclosing class for packaging convenience.

Kal
  • 24,724
  • 7
  • 65
  • 65
  • 4
    Why shouldn't `OuterClass.InnerClass.staticMethod()` be able to work? An inner class is simply a normal class with a hidden reference to the enclosing class instance. – Lawrence Dol May 10 '11 at 05:24
  • 1
    @Software Monkey -- Good point. Here is the catch -- The classloader does not have access to the inner class except in the context of an instance of the outer class, thats why you can't do something like what you mentioned above. – Kal May 10 '11 at 14:56
  • 1
    So it's a problem with Java then. Since other languages don't have this constrain. – Pacerier Nov 14 '11 at 06:06
  • You are wrong, @Kal. The classloader *absolutely* has access to the inner class without an instance of the outer class. It is only *instances* of the inner class which can't exist without an instance of the outer. This is trivial to verify by obtaining a `Class` instance via `OuterClass.InnerClass.class`, which can then be reflected over. The decision to disallow static methods on such classes may have been a conceptually/design motivated one. Personally I don't think it was a great one, but regardless, the reasons you gave are nonsense. – Elias Vasylenko Jul 15 '15 at 15:05
  • If you make a private, non-static, nested class, doesn't 'private' imply that the outer class already has an instance if accessing an instance of the nested class? – Phillip Jul 10 '16 at 22:22