Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Cool compiler error message with generics

Thread view: 
HK - 19 Jul 2005 10:08 GMT
import java.util.*;
public class Bla<T> {

 private List<T>[] ary = new ArrayList[10];

 private class InnerClass<T> {
   public void boo() {
     List<T> et = ary[0];
   }
 }
}

Bla.java:8: incompatible types
found   : java.util.List<T>
required: java.util.List<T>
     List<T> et = ary[0];

It took me some time to realize that the <T> of
InnerClass is a completely different one from
the <T> of Bla. I wonder if the compiler should
rather complain that type variable <T> is
redefined at InnerClass<T>.

 Harald.
Stefan Schulz - 19 Jul 2005 10:11 GMT
> It took me some time to realize that the <T> of
> InnerClass is a completely different one from
> the <T> of Bla. I wonder if the compiler should
> rather complain that type variable <T> is
> redefined at InnerClass<T>.

Why, it is only consistent with fields, which may have the same name as
well, but different types.

Signature

You can't run away forever,
But there's nothing wrong with getting a good head start.
          --- Jim Steinman, "Rock and Roll Dreams Come Through"
         

Thomas Hawtin - 19 Jul 2005 13:50 GMT
> import java.util.*;
> public class Bla<T> {
[quoted text clipped - 18 lines]
> rather complain that type variable <T> is
> redefined at InnerClass<T>.

Obviously not a good idea to reuse generic parameter names in nested
classes. I think the problem is defining lint warnings consistently that
wouldn't complain about, say, having a local and a field with the same name.

You can get something similar at runtime with misuse of class loaders.
If the same class file is loaded by two loaders you can get a class cast
exception claiming that you cannot cast com.mycorp.MyClass to
com.mycorp.MyClass.

Tom Hawtin
Signature

Unemployed English Java programmer



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.