[quote] Vorax, it sounds to me you literally dont know what to say and are just jumping on the hate wagon. At least the others who disagreed has the intelligence to include a point of argument…
[/quote]
I didn’t read the other posts when I replied to yours.
So…let me elaborate:
[quote]inner classes is the most pointless and bug-prone feature in Java
[/quote]
They are far from pointless and useful for things like:
- Implementing listeners (as you did)
- Implementing visitors
- Implementing traversal handlers
- Implementing runnables to spawn one shot threads
- Implementing data objects
- Much more…
As for bugs: In 9 years of Java programming I have seen very few (I can’t even recall any) bugs caused due to the use of inner classes.
There are lots of ways to make bugs, but inner classes, unless you are have no idea how to use them, are no more error prone then anything else in the language and less so then many other key features. For example, casting, subclassing and scope all cause FAR more bugs then inner classes. Cast to the wrong type, overload a parents incorrectly scoped variable or method, misrepresent the functionality of a method are all much more common problems…maybe we should all be using C?
Most Java newbies have no idea about inner classes and even fewer know you can use them to access the parents members.
[quote] You’d be better off by just using two seperate classes…
[/quote]
But what if you don’t want the second class to be represented in the package because it has no meaning except to the class that uses it? That is the point of inner classes: Cleaner API’s and packages and REDUCED errors from exposing classes that shouldn’t be used by others.
Should I do this:
public interface CallBackThing {
public void callBack();
}
public class Test {
public Test() {
…
callsBackThings.add( new CBHandler() ) ;
}
}
public CBHandler implements CallBackThing {
public void callBack() {
System.out.println("I was called back on behalf of “Test”);
}
}
…What if someone now instantiates and uses CBHandler? It is useless to them and is cluttering up the API. It is specific to Test and means nothing to anything else…picture there are dozens of classes like Test, should their be dozens of CBHandler type classes? What if we want to implement several interfaces, should Test be forced to implement all of them even if they make no sense to the class thus corrupting its purpose and causing it to loose functional decomposition? What if you don’t want Test to be used as one of the interfaces it is exposing? Since it implemented it, there is nothing stopping someone from doing so. If it implemented CallBackThing, it has no control and could be added by somone else to something that takes a CallBackThing as a parameter…more bugs.
In Java, the inner class way is better:
public class Test {
public Test() {
callsBackStuff.add( new CBHandler(this) ) ;
}
CBHandler implements CallBackThing {
public void callBack() {
System.out.println(“I was called back on behalf of Test”);
}
}
}
Now no one using Test has to be concerned with it’s internal working which means it has better encapsulation (also no one can cast it to anything other then its type, purposefully implemented interfaces or Object), a key goal of OOP. The same goes for the inner class, even it’s type has no meaning outside the parent class so it can not be used incorrectly when passed by it’s interface. Inner classes aren’'t unobject oriented, they are VERY object oriented.
Inner classes alllow more complete encapsulation while at the same time preventing packages from being cluttered with .java files that have no meaning and should never be used outside the class they were built for…if they can be used outside that class, then they should be public and become first class citizens of the package.
This is how I see and use inner classes and it’s why I had such a problem with your statment.
Edit: My lack of a reply had nothing to do with my intelligence, but thanks for the jab. I didn’t reply because I have beeing doing OOP since 1987 and have been explaining things like this for almost 20 years and am tired of doing it. To me your remarks seemed like that of a newbie, not of someone with a point and I have yet to see one from you that justifies your stance.