Inner Classes

Hi,

I understand that inner classes have an additional privilege of having access even to the private variables and methods of a class. Apart from this, why would anyone want to use an inner class in their program? I did read explanations pertaining to scenarios where inner classes have been used to form "animations", but I still am struggling to understand their real significance.

Any pointers or explanations will be appreciated.

Thanks,

[468 byte] By [Javajockeya] at [2007-11-26 18:38:24]
# 1
One possible explanation is if you have a class that is only used by another class, you can make it inner but it's a pure design choice
manuel.leiriaa at 2007-7-9 6:12:28 > top of Java-index,Java Essentials,New To Java...
# 2

For me, the biggest reason is laziness. For a small class, it's easier to write it inline that to set it up as a top-level class. This is very common with listeners in Swing. For example, the following sets up a listener that adjusts another components enabled state:

check.addItemListener(new ItemListener(){

public void itemStateChanged(ItemEvent e){

field.setEnabled(e.getStateChange()==ItemEvent.SELECTED);

}

});

You could write that as a separate class:

class EnablerItemListener implements ItemListener {

private Component field;

public EnablerItemListener(Component field) {

this.field = field;

}

public void itemStateChanged(ItemEvent e){

field.setEnabled(e.getStateChange()==ItemEvent.SELECTED);

}

}

But unless you have a specific need to do that, why bother?

DrLaszloJamfa at 2007-7-9 6:12:28 > top of Java-index,Java Essentials,New To Java...
# 3
It's not just laziness though. When inner classes are appropriate, they make the code much cleaner, as your example shows. Take that example and multiply it by the many (maybe dozens) of little individual event handlers you'd need for a GUI.
paulcwa at 2007-7-9 6:12:28 > top of Java-index,Java Essentials,New To Java...
# 4

> It's not just laziness though. When inner classes

> are appropriate, they make the code much cleaner, as

> your example shows.

I'd like to believe that, but with my example, the main class becomes:

check.addItemListener(new EnablerItemListener (field));

If you follow this pattern you end up with many little listener classes but the larger classes become cleaner because all their inner classes go away.

The total amount of code increases because you are passing parameters to these new listener classes, but that is amortized across many files.

All that being said, I'm disinclined to switch to this style, so I'm willing to believe the original way is better :-)

DrLaszloJamfa at 2007-7-9 6:12:28 > top of Java-index,Java Essentials,New To Java...
# 5
> Any pointers or explanations will be appreciated.Both variables and types should be defined at the narrowest scope possible. For exampel if you use a class inside a specific method only then define it within that method.
DrLaszloJamfa at 2007-7-9 6:12:28 > top of Java-index,Java Essentials,New To Java...