•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,607 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 863 | Replies: 7 | Solved
![]() |
Herbert Schildt in The Complete Reference Java 2 says,"Because it is static, it must access the members of its enclosing class through an object. That is, it cannot refer to members of its enclosing class directly. Because of this restriction, static nested classes are seldom used."
Can we use static nested classes practically? If yes, please demonstrate it with help of an example program.
Can we use static nested classes practically? If yes, please demonstrate it with help of an example program.
I don't think that they can be useful. So I am wondering whether they can be practically implemented or not. I still do not know whether they have some practical application or they are just a theoretical concept. Someone please help.
> Can we use static nested classes practically?
Look at the source code of the 'Entry' inner class of 'HashMap'. The source can be found in the JDK directory in src.zip.
Look at the source code of the 'Entry' inner class of 'HashMap'. The source can be found in the JDK directory in src.zip.
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
They can be useful as small data structures or "helper" objects that only have applicability in the context of their enclosing class. S.O.S. posted an example of such a case above. The 'Entry' is really only useful with the HashMap. It could be published as a full public class, but if it is not used by other classes then it just clutters the API. As a static inner class of HashMap, its usage is clearly scoped.
I understood now. Thank you!
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
one word of caution: don't trust anything you read in Herb's book unless and until you find it confirmed elsewhere.
It's not all bad, but there is a large number of glaring errors caused by his fundamental misunderstanding of some core concepts of the Java language and its runtime environment.
While he is right that static nested classes have restrictions, they can be useful nonetheless. Particulary they can be useful when employed in combination with nested interfaces inside interfaces and abstract classes to provide callback mechanisms.
It's not all bad, but there is a large number of glaring errors caused by his fundamental misunderstanding of some core concepts of the Java language and its runtime environment.
While he is right that static nested classes have restrictions, they can be useful nonetheless. Particulary they can be useful when employed in combination with nested interfaces inside interfaces and abstract classes to provide callback mechanisms.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- C++ Performance Tips (C++)
- Python Tuples and Mysql Query (MySQL)
- Python tuples and Sql query (Python)
- Class in a class... (Java)
- Java Client/Server (Java)
- Which type of class is not instantiated? (Java)
- MySQL nested query / joined query conversion help (MySQL)
- Threading question... (Java)
Other Threads in the Java Forum
- Previous Thread: Diplaying floating point values
- Next Thread: bit pattern of an audio file



Linear Mode