User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Question Query about static nested classes

  #1  
Nov 17th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Query about static nested classes

  #2  
Nov 17th, 2007
So have you come up with an answer to this homework question yet? When do you think they could be useful?
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Query about static nested classes

  #3  
Nov 18th, 2007
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.

Originally Posted by Ezzaral View Post
So have you come up with an answer to this homework question yet? When do you think they could be useful?
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,009
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Query about static nested classes

  #4  
Nov 18th, 2007
> 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.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Query about static nested classes

  #5  
Nov 18th, 2007
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.
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Query about static nested classes

  #6  
Nov 18th, 2007
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.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Query about static nested classes

  #7  
Nov 18th, 2007
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Aug 2007
Posts: 74
Reputation: lookof2day is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 9
lookof2day lookof2day is offline Offline
Junior Poster in Training

Re: Query about static nested classes

  #8  
Nov 23rd, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC