Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

inner class can't have static member-why?  RSS feed

 
pooja jain
greenhorn
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why inner class can't have static members ?
Any logical answer .

Thanks in advance .
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think so we have any say on this?
Its just a rule and should be implemented
Static members are only permitted in static classes or top level classes.

Normally even for instance initializers u cant have statics defined in it.
public class InnerClass {
{
static int k = 1;
}
}
The above code would give u a compilation error.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Static members are only permitted in static classes or top level classes.


...or interfaces (though, to do so, suggests broken design).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't static member declarations in interfaces allowed in precisely the same cases that they're allowed in inner classes? That is, they're only allowed for compile-time constant fields. Well, interfaces may also contain static member typesas well. But you weren't including them in the "broken design" comment, were you? I see no problem with Map.Entry, for example. I do agree that constants in interfaces are usually a bad idea, though I don't think they're necessarily always bad. If the constants are used as an intrinsic part of the intended API, rather than just as a convenient way to import a bunch of constants, that could be acceptable. Though I can't think of any good example of this in standard APIs, so that's probably a clue the need for this sort of thing is extremely rare.

To the original question: at one level, the answer is: because the Java Language Specification says so, and the compiler won't let you violate this rule. But at another level - why did they make this decision? I think it comes down to two factors: (1) there's no need for static members in inner classes, since you can always declare it within an outer class instead, and (2) they would be potentially confusing, since some people might expect an inner static field to be bound to the outer instance (a different outer instance might have a different value for the value of its inner class' static field) and other people might expect a single value for the field across all outer instances. Both of these effects are probably better implemented using other techniques, using either an outer class field, or an outer instance field. There's no real benefit to declaring static fields inside an inner class - only more confusion.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aren't static member declarations in interfaces allowed in precisely the same cases that they're allowed in inner classes? That is, they're only allowed for compile-time constant fields.

No.

Perhaps you are thinking of inner interfaces of static nested classes?


Well, interfaces may also contain static member typesas well. But you weren't including them in the "broken design" comment, were you? I see no problem with Map.Entry, for example.

I see a huge problem, but that's a long, dragged out debate.
In the end, I will be called a "purist" and you will be the "practical person", though I will argue that purism has a significant practical value and you will argue that your definition of "purism" differs from mine. Then we will debate the definition of "purity" in software design. Then someone who doesn't quite understand the debate will make an irrelevant comment that causes the debate to deviate from its original intention, and nobody will learn anything.

Shall we skip that bit, and head to the pub now?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Tony]: Perhaps you are thinking of inner interfaces of static nested classes?

No, I was thinking of regular interfaces - didn't realize that they could be initilized with expressions that weren't compile-time constants. Thanks for pointing that out. Clearly interface fields are still implicitly final and static, but this makes them a good deal more flexible - mutable, even. I'm having a hard time imagining why the JLS allows this, as it seems pretty odd. With the advent of enums I can imagine it's potentially useful to allow non-mutable enums as part of an interface. But I can't see how it could ever be a good idea to have a mutable object as an interface member. Strange...

Note that in JLS terminology, there is no such thing as an inner interface. And a nested interface may declare a member field the same way a top-level interface can (now that I know it doesn't have to be a compile-time constant). That's because the field is implicitly static anyway:

---

Re: Map.Entry:

I see a huge problem, but that's a long, dragged out debate.

Not necessarily, as it's not that big an issue to me. I'd be curious to learn what you think is wrong with it, but I doubt it would inspire a prolonged battle from me.

Shall we skip that bit, and head to the pub now?

Sounds good.
[ February 13, 2005: Message edited by: Jim Yingst ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!