943,577 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1678
  • Java RSS
Nov 17th, 2007
0

Query about static nested classes

Expand Post »
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.
Similar Threads
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 17th, 2007
0

Re: Query about static nested classes

So have you come up with an answer to this homework question yet? When do you think they could be useful?
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Nov 18th, 2007
0

Re: Query about static nested classes

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.

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
So have you come up with an answer to this homework question yet? When do you think they could be useful?
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 18th, 2007
0

Re: Query about static nested classes

> 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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Nov 18th, 2007
1

Re: Query about static nested classes

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Nov 18th, 2007
0

Re: Query about static nested classes

I understood now. Thank you!
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 18th, 2007
1

Re: Query about static nested classes

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Nov 23rd, 2007
0

Re: Query about static nested classes

I have used static nested classes in struts to populate a html list from database objects. If you are using struts you might find them handy.
Reputation Points: 16
Solved Threads: 11
Junior Poster in Training
lookof2day is offline Offline
83 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Diplaying floating point values
Next Thread in Java Forum Timeline: bit pattern of an audio file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC