This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
A class becomes a static class only when it is declared to be static and top-level classes cannot be static.
Inner classes can be static.
If you are not laughing at yourself, then you just didn't get the joke.
Joe Shy
Ranch Hand
Joined: Dec 09, 2005
Posts: 38
posted
0
Sathya,
I like to think of static classes in a sense as a "global" object, function, var... Everytime you call a constructor on a non-static object, you get a 'new' object, and all these 'new' objects, if permitted would all call the same single static object throughout the apps runtime. Guys correct me if I am wrong in this thinking, but its an easy way for me to keep things straight.
Cheers,
Joe
SCJP, SCJD(In Progress)<br /> <br />"They whom make no mistakes usually make nothing at all."
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
Originally posted by Joe Schider:
I like to think of static classes in a sense as a "global" object, function, var... Everytime you call a constructor on a non-static object, you get a 'new' object, and all these 'new' objects, if permitted would all call the same single static object throughout the apps runtime. Guys correct me if I am wrong in this thinking, but its an easy way for me to keep things straight.
It sounds to me like you are confusing a static class with a static field. B is a static class, and whether or no field b is defined to be static is orthogonal to B being a static class.
There is no emoticon for what I am feeling!
Joe Shy
Ranch Hand
Joined: Dec 09, 2005
Posts: 38
posted
0
Aaaah, I see, I was thinking static method. Thanks.
Originally posted by Arun Kumarr: ... Inner classes can be static.
Be careful with the terminology...
According to the JLS, "A nested class is any class whose declaration occurs within the body of another class or interface." And an "inner class is a nested class that is not explicitly or implicitly declared static."
So a nested class can be static, but an inner class is -- by definition -- not static.
Note that you have been misled earlier with "a static class is always an inner class". The correction is either: "a static class is never an inner class" "a static class is always a nested class"
It mentions "Static Inner Class".Kindly let me know.
Regards, Sathya
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
"Static inner class" is a term from the early days of Java, when Sun wasn't as clear about it's terminology. Today, it officially is an oxymoron...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
Alright, now I'm confused:
* Top level class -- that's an obvious category, even if it is defined as the opposite of... * Nested class -- one defined within the body of another class or interface. Okay, that clear, too. A class is either a top level class or a nested class, but never both. So far so good...
Where it gets tricky is the defintion of inner. For me, defintions should have a purpose -- should be useful -- and inner seems too slippery. I turned to the doughty, redoubtable, indeed inestimable and indescribable Tony Morris for enlightenment, and read his article, linked above. He gives this example:
Following is an example of a nested class that is a local class and is an inner class. A compile-time error occurs if attempting to make a local class that is not an inner class.
Speaking of compile-time errors, I'm getting one on line 05! Okay, that private should go AWOL. But what does it mean to try to "make a local class that is not an inner class"? According to the JLS (3rd edition, 8.1.3):
An inner class is a nested class that is not implicitly or explicitly declared static. Inner class may not declare static initializers or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields.
(The second half of that quote gives a way to test inner-ness.)
In section 14.3, local class is defined:
a local class is a nested class that is not a member of any class and that has a name. All local classes are inner classes.
Again, if all local classes are inner classes what does it mean to try to make a local class not an inner class? Going back to the example code above, what if I define the nested class in a static context?
All the commented code would generate compile-time errors: the first three (the static initializer, the member interface and the static field) because NestedInner is an inner class, so says my compiler. But I can't invoke nonStaticMethod from m because "non-static method nonstaticMethod() cannot be referenced from a static context". Hey, the JLS said that a inner class can't be implicitly declared static. Isn't NestedInner implicitly static because it is defined inside a static method? How else could I instantiate it at the end of that static method?
In summary, I thought inner class had a simple useful implication: its instances had an implicit field to the immediately enclosing class, you know, TopLevel.this, but that doesn't seem to be what's happening in this example. Is there terminology -- useful terminology -- that distinquishes between classes whose instances have this implicit field and classes that don't? Now that would be useful, at least slightly useful...
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
Jim Yingst has emailed me with his suggested error corrections. Although I haven't looked at them in detail, they look legitimate to me. I apologise for the confusion - I will correct the situation ASAP.
Jim, Do you think it is worthwhile to post your email for now?
Edit: Removed the DDL since it was too unwieldly. [ December 23, 2005: Message edited by: Tony Morris ]
Tony Morris
Ranch Hand
Joined: Sep 24, 2003
Posts: 1608
posted
0
I have rushed together an attempt to correct. This is a preliminary attempt that requires proof reading, etc., which unfortunately, I don't have time for. Any comments or suggestions are always welcome emailed to tmorris at tmorris.net
[Tony]: Jim, Do you think it is worthwhile to post your email for now?
I guess my response now might have been more useful if I'd been online to see your question earlier, before you'd had a chance to modify the question text. Now that you've got a newer version up it's probably easiest to focus on that; the text of my e-mail would be harder to interpret now since it's expressed relative to an earlier version of your text. If you want to repost some or all of the e-mail you're welcome to, but it's probably not so useful now anyway.
[Jeff]: But what does it mean to try to "make a local class that is not an inner class"?
I'm guessing this refers to the fact that it's a compile-time error to declare a local class as static. I can't think of a way to try to make a local class implicitly static. You could make it a member of an interface, but then it's not a local class at all, it's a static member class, and there's no compile error.
[Jeff]: Isn't NestedInner implicitly static because it is defined inside a static method?
No. That might sound intuitively likely, but "defined in a static context" != implicitly static. They're two different things, though they do have in common the fact that there's no enclosing instance.
[Jeff]: How else could I instantiate it at the end of that static method?
Um, with a constructor, like you show? I've probably missed the point of your question here. Or perhaps your point is addressed in this next section:
[Jeff]: In summary, I thought inner class had a simple useful implication: its instances had an implicit field to the immediately enclosing class, you know, TopLevel.this, but that doesn't seem to be what's happening in this example.
That would have been a useful definition, and indeed inner classes are often incorrectly characterized this way in various books. Unfortunately that's not quite the definition Sun actually used in the JLS.
[Jeff]: . Is there terminology -- useful terminology -- that distinquishes between classes whose instances have this implicit field and classes that don't? Now that would be useful, at least slightly useful...
There's nothing as concise as "inner" would have been. A class that doesn't have a containing instance (an outer "this" reference) is either a top-level class, a static member class, or an inner class defined in a static context. That's the official term for the last category. Such a class is inner, not static, not even implicitly static, but it is "defined in a static context". Which is like being static in some ways, except it's not. Clear as mud, I know. Meanwhile "regular" inner classes (the ones with an enclosing instance) can't be concisely specified either - you have to say something like "an inner class not defined in a static context." Well, in the case of member classes, you can say "inner member class" and that can't possibly be in a static context, so inner member classes always have an enclosing instance. But if you want to also talk about local and anonymous classes too, you've got to spell out what you mean the long way.
Aside to Tony - the corrections look good (though like you, I haven't gone over them really carefully yet). But one thing I notice is you now seem to be implying anonymous classes are included under local classes, is that right? That's another thing that would make sense, I think, but it doesn't seem to be how Sun actually defined things in the JLS. As far as I can tell, local and anonymous classes are always two different things. I also thought your earlier version of this article kept the two categories separate, which I agreed with. I hope I didn't somehow give the impression in my e-mail that I thought anonymous should be included within local - that wasn't my intent.
The diagram is cool, probably a lot clearer than having a lot of text. I might want to rearrange the order a bit to see a progression from top-level to static member to inner member to local and anonymous, since that's often the order people learn about them, and it puts similar behaviors next to one another:
Additionally local and anonymous could be split according to whether or not they were defined in a static context:
but that may be easier to discuss separately instead.
"I'm not back." - Bill Harding, Twister
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
Jim, Thanks a lot, that was a useful post. I'm wondering if we can prevail on you to ring the changes and post short examples. The thing I find the fuzziest is the distinction between "implicitly static" and "defined in a static context". Could you give 6 examples or indicate that the combination is impossible: 1. Member class: implicitly static 2. Member class: defined in a static context 3. Local class: implicitly static 4. Local class: defined in a static context 5. Anonymous class: implicitly static 6. Anonymous class: defined in a static context
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
Ok, these are off the cuff, minimal examples, and I'm on a very slow connection with no JDK handy, so I may manage to insert some silly compilation error along the way. The ones that say "impossible", of course I could just be forgetting something. But here we go...