de.velopmind | The Det about Programming

June 5, 2008

Java: why the heck compiler doesn’t check ?

Filed under: Experiences, Java — de.velopmind @ 8:21 am

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??

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: