The Det about Programming

Just another progger’s blog

Java: why the heck compiler doesn’t check ?

Well, why do we use typing in our coding?   To get typechecks by the compiler, I thought.

But the compiler indeed has to know, what we want or expect.  So we give even more type information, namely by  casting and parametrisation (Generics).

But why the heck is this “fine” Java compiler not able to detect such a simple typo?

Collection<AssociationEnd>  lChildViews  =   lView. getChildViews();

for (AssociationEnd iFacade: lChildViews) {
    WidgetAssociationEnd lFacade = (WidgetAssociationEnd) lChildViews;

Well, indeed the assignment should take and cast the reference in  iFacade, not in lChildViews.

The code above resulted in a nice ClassCastException  … at runtime!

Now I am working with so much type information, and the compiler is not able to check that WidgetAssociationEnd is no subtype of Collection?   What the hell is this??

June 5, 2008 - Posted by thedetdev | Experiences, Java | | No Comments Yet

No comments yet.

Leave a comment