aspose file tools*
The moose likes Beginning Java and the fly likes Practical/real world situations where inner classes are useful? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Practical/real world situations where inner classes are useful?" Watch "Practical/real world situations where inner classes are useful?" New topic
Author

Practical/real world situations where inner classes are useful?

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Why do we need inner classes ? Can anyone give me a practical/real world situation where they could be useful ?

SCJP 6. Learning more now.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
I didn't look up the definition of useful, but I imagine it's broad enough to cover most occasions when a thing contributes to the accomplishment of an objective desired by the user. Inner classes can be used, they don't have to be, to accomplish the programmer's objectives, so they're useful. Further, the programmer wouldn't use inner classes if they weren't 'useful.' You either need to define what you mean by useful or accept the fact that they just are.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Rahul Sudip Bose wrote:Why do we need inner classes ? Can anyone give me a practical/real world situation where they could be useful ?


Used in building GUI applications using Swing- Adding event handlers.


Mohamed Sanaulla | My Blog
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

please see this post.

http://www.coderanch.com/t/533292/java/java/Collections-sort-guidance-code#2419460
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

there can be a lot of examples ... for suppose

you have two classes

1. Animal
2. Dog

Now let's say Dog is your core class that you have to work with ... but just for some requirements you need some generic functionality and you do not want to put that in the Dog class ...

here you go ... you can make another class Animal (inner class) and write your generic code in it ...


and access it from within the Dog class itself ...


http://plainoldjavaobject.blogspot.in
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Lalit Mehra wrote:there can be a lot of examples ... for suppose

you have two classes

1. Animal
2. Dog

Now let's say Dog is your core class that you have to work with ... but just for some requirements you need some generic functionality and you do not want to put that in the Dog class ...

here you go ... you can make another class Animal (inner class) and write your generic code in it ...


and access it from within the Dog class itself ...


Not an example for inner class. Its - Super class- Sub class relationship.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Mohamed Sanaulla wrote:

Not an example for inner class. Its - Super class- Sub class relationship.



brother i have explicitly written (inner class) please check ...

Animal and Dog doesn't always need to be declared as a superclass subclass relationship ...
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Lalit Mehra wrote:but just for some requirements you need some generic functionality and you do not want to put that in the Dog class


You say its a generic functionality and want to create an Inner class for that? How would other classes make use of the Generic functionality? If there are no other classes using this- then its not a generic functionality.

Lalit Mehra wrote: brother i have explicitly written (inner class) please check ...

Though it can be implemented. I feel its Not real world situation example. And do you think the example you have provided is useful?
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

yes is do think so ... and by the term generic i meant something that shouldn't be coded in the Dog class ... and i have also written there is no other class to use that functionality ...

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Lalit Mehra wrote:there can be a lot of examples ... for suppose

you have two classes

1. Animal
2. Dog

Now let's say Dog is your core class that you have to work with ... but just for some requirements you need some generic functionality and you do not want to put that in the Dog class ...

here you go ... you can make another class Animal (inner class) and write your generic code in it ...


and access it from within the Dog class itself ...


Please show the complete code and tell us which real world app could use this code ? Or if you have written some code other than the dog-cat example, please show that as well.
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

i asked you to suppose it ...

anyways ... just for the sake of providing real world examples ... go through the java API documentation for Collections and Generics ... JAVA API

for the best of all ...

keep coding
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33


Lalit Mehra wrote:
anyways ... just for the sake of providing real world examples ... go through the java API documentation for Collections and Generics ... JAVA API

How would Java API provide example for Inner classes? If at all you would ask to refer to the source code- that might have been useful

Rahul, I could just recall that- Lot of places in JDK source code inner classes have been used. You might want to check the Stream related classes in Java IO package (Not sure about the exact classes which use them, someone might be able to point few example classes which use).
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

Mohamed Sanaulla wrote:

How would Java API provide example for Inner classes? If at all you would ask to refer to the source code- that might have been useful



there are many classes in JAVA API which have inner classes .... anyways leave it ... either i'm not being able to understand you or the same is the case with you ...
 
 
subject: Practical/real world situations where inner classes are useful?