We help IT Professionals succeed at work.

Inner class, good programming practice?

jerntat
jerntat asked
on
Hello, just want to ask if it is a good programming practice to declare a class inside a class, something like:

public class abc
{
 private a;
 pricate b;

 public abc()
 {
 }
 
 class def
 {
   private d;

   public def()
   {
   }
 }

}

recently i am maintaining a code done by someone else and he uses alot of inner class and I find it very messy. Why couldnt the inner class be declared as a stand alone class? whats the advantage of using inner class? Is it a godd programming practice or should it be avoided?

Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I'm starting to think that it should be avoided and that as you say the class should be written as a standalone class.
There are times when it is convenient to use inner classes for things like listeners, where the class is just a basic adapter. But I think you get lazy and get into the habit of using inner classes in more places than they are necessary.
Using inner class will be useful for garbage collector.

Inner classes will have soft references instead of strong. SO if the jvm runs on low vertual memory the garbage collector will release these objs but cant strong references. I heard some where but not sure. I posted it for get some more ideas, thats it.

thank u

I agree with objects. Inner classes makes sense with listeners or some internal service functions to keep the class api "smaler".
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
They also allow you to 'hide' your listener interfaces by not needing to declare them as public.
Senior Engineer
Commented:
Inner classes have a number of benefits.

For one, it encourages you to make objects of things that you might not have previously considered. Sometimes it is useful to encapsulate an algorithm as a separate class. You want to keep the implementation private so it wouldn't make sense to make it a top-level class. The logical extension of this idea is that you might have multiple implementations that you could swap in on demand -- all private. This is typical of Strategy pattern implementations -- a concept central to my initial implementation of the log4j PatternLayout. I used inner classes because I didn't believe it was necessary or useful to expose the pattern rendering classes.

Since instance inner classes have access to their enclosing class's fields, inner classes can be used to elegantly implement some design patterns. One prime example of this is Iterator. Each concrete implementation of List has a corresponding implementation of an Iterator because what the Iterator does will be different for an ArrayList say v. a LinkedList. And the Iterator will need to have access to the private fields of the List. You couldn't accomplish this if you had an ArrayListIterator class as a top-level class. You couldn't say "new ArrayListIterator(ArrayList)" and have the resulting Iterator object accessing private members of the ArrayList. Instead, ArrayList has an inner class that implements the Iterator interface. You get an instance of that Iterator using the ArrayList's implementation of the List.iterator.

Inner classes provide greater depth to class organization than possible through simple packaging. Inner classes are scoped to a namespace defined by the outer enclosing class. This is true regardless of the visibility of the inner class or whether it is static or instance.

As mentioned by other posters, inner classes are great for callbacks. Many times these are anonymous but that does not need to be the case. As a matter of style, many programmers think that too many anonymous inner classes are cluttering. They prefer to use member classes for this purpose.

Jim

Author

Commented:
Sorry for late reply. I have been doing some readings and trying to understand what you guys are saying. So inner class and anonymous inner class are perfectly alright to use, its just the problem of whether it is used correctly to organized the code in a clear nad easy to understand manner by following natuarally the logic of the problem the code is to solve. However, like many other things, inner class could easily be abuse and misused and making the code look messy. Thanks guys.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.