Prev: Why can nsmc, local classes or anonymous classes have static members ?
Next: how to kill a java thread by force?
From: Arne Vajhøj on 12 Dec 2009 16:49 On 12-12-2009 16:27, Rit wrote: > I could not understand why nsmc, local classes or anonymous classes > cannot have static members ? > > Can some throw light on that ? Anonymous classes can not have static methods because those can not be called by anyone. I don't know what you mean by nsmc and local classes. Arne
From: markspace on 12 Dec 2009 16:52 Rit wrote: > Hi All, > > I could not understand why nsmc, local classes or anonymous classes > cannot have static members ? > > Can some throw light on that ? Well, I don't know, but I'll hazard a guess or two: 1. Conceptually, non-static member classes, local and anonymous classes are all "a part of" the enclosing class. Thus they "should" be using the enclosing class for any static members. 2. For future potential optimization, preventing static members might allow future JVMs to take some short cuts with inner class objects, and initialize the objects more quickly or otherwise do away with one or more steps involved in creating their class object. Since static members weren't needed (see 1), they were proscribed.
From: Patricia Shanahan on 13 Dec 2009 00:18 Roedy Green wrote: > On Sat, 12 Dec 2009 13:27:01 -0800 (PST), Rit > <ritesh.shukla10(a)gmail.com> wrote, quoted or indirectly quoted someone > who said : > >> I could not understand why nsmc, local classes or anonymous classes >> cannot have static members ? >> >> Can some throw light on that ? > > Inner classes are not permitted to have static methods or fields. That > is not quite true. They are allowed static final compile time > constants, which are treated as if there were literals. Sorry I don�t > know why the restriction. Nobody I have asked knows why. This is > probably the single most annoying fact about nested classes. If inner > classes need statics, they have to get the outer class to hold them, > or you have to use static nested classes or you have to inherit the > static fields. Oddly, inner classes are permitted to extend classes > that do have static methods and fields. > > see http://mindprod.com/jgloss/nestedclasses.html My guess is that the objective is to allow the more light weight classes to be effectively unloaded without affecting anything. Patricia
From: Lew on 13 Dec 2009 00:22 Roedy Green wrote: > static fields. Oddly, inner classes are permitted to extend classes > that do have static methods and fields. That's not so odd considering that static members aren't really inherited, just accessible. -- Lew
From: Arne Vajhøj on 13 Dec 2009 20:32
On 12-12-2009 23:25, Roedy Green wrote: > On Sat, 12 Dec 2009 16:49:45 -0500, Arne Vajh�j<arne(a)vajhoej.dk> > wrote, quoted or indirectly quoted someone who said : >> Anonymous classes can not have static methods because those >> can not be called by anyone. > > You could not call them from outside, since they would not have a > name, but why could you not call such static methods within the > anonymous class? I guess you could do that. But then what benefits would that static method provide that a similar non-static method would not provide? Arne |