• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

What is a static class???Kindly let me know.

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what does one mean by a static class???

Regards,
Sathya
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are some answers:

Static class declarations
Java Tip 106: Static inner classes for fun and profit
 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So,if someone mentions static class, does it always mean static inner class???

If a class contains only static methods,does it become a static class???

Regards,
Sathya
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Joe Shy
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaaah, I see, I was thinking static method. Thanks.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Ref:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See if this helps:
What is the difference between a nested class, an inner class, a local class, an anonymous class and a member class (or interface)?
http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true

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"

Good luck
 
Arun Kumarr
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah!!

I was careful in writing top-level class than outer class but not careful enough in writing nested class than inner class.
 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
kindly find the below article.

http://www.javaworld.com/javaworld/javatips/jw-javatip106.html

It mentions "Static Inner Class".Kindly let me know.

Regards,
Sathya
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

If anyone has the burning desire to make the corrections to the data themselves, I have posted the relevant field from the database DDL here: http://pastebin.com/477081
http://jqa.tmorris.net/GetQAndA.action?qids=67&showAnswers=true

Sorry again for any confusion.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

1. Member class: implicitly static

Bar is an implicitly static member of Foo.

2. Member class: defined in a static context

Impossible.

3. Local class: implicitly static

Impossible.

4. Local class: defined in a static context


5. Anonymous class: implicitly static

Impossible.

6. Anonymous class: defined in a static context
 
reply
    Bookmark Topic Watch Topic
  • New Topic