This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java's Inner Classes - Does those Help or is better Avoid those?  RSS feed

 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which potential from any kind of those Java's Inner Classes doing a comparision with a normal/convential class that we used to use everyday? Is there any situation that the use of those is commendable/laudable? Does Anybody here use those an why?

* Inner Class
* Static Class
* Nested Class
* Anonnymous Class
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a normal application, I would think that their uses are limited. However, there are many ways in which they could be useful, and that alone warrants their inclusion in the JLS.

Concrete examples of that, are, for instance, event listeners. They are heavily tied to the class that usually encloses them, and on many accounts need to be able to access their private variables and / or methods. You could put all your listeners outside your class in a separate class, but in many cases, that would require you to loosen your encapsulation and pass your Object reference to the other class, which is tight coupling and bad design. It is better for such classes to be used internally, rather than externally.

There are of course, a wide range of different cases in which inner classes can be useful, but just know they are not there just to make your java studies more difficult. They are a design option - and perhaps some designers will rarely use them, other designers will use them constantly. Either way, it's good to have the choice.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not used inner classes in my code till now.
But it think the inner classes is used in most of the time in GUI applications.
As in GUI,
There are lot of component in a class that are required to perform a different action and if we are going to make a different class for a each component that there will be more and more if-else conditions and when this kind of situation occurs this will be considered a bad design.

so in GUI we always use a inner class,mostly anonymouse inner class

bad design..


this is a small case but in case of projects,it could become more large depending upon requirements.

for Good Design
you can also see like this
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class extending an abstract class or implementing an interface, cannot re-implement the method the abstract method... It can be done using the inner class which implements interface or extending abstract classes... Mostly Inner Classes are used for defining Event Listeners in Java Swing and Defining the Comparators...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am just wondering where i have seen a class implementing an other class.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!