| | |
Query about static nested classes
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread 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.
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.
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.
![]() |
Similar Threads
- 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
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows






