Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6343995

"incompatible types" error when files are compiled separately

XMLWordPrintable

    • generic, x86
    • generic, windows_xp

      I have faced a weird issue while using generics (on JDK 1.5.0 update 3 for Windows).

      Consider the following simple example.

      public class Foo {
           public class FooInner<T> { }
           public <T> FooInner<T> getInner() { return null; }
      }
       
       
      public class Bar {
        final Foo foo=new Foo();
        
        public Foo.FooInner<Void> i1=foo.getInner(); // incompatible types error on separate compilation
        public Foo.FooInner<Long> i2=foo.<Long>getInner(); // incompatible types error on separate compilation
      }




      If I compile the both files together

      $ javac Foo.java Bar.java

      they are compiled fine without any errors or warnings (exactly as I expect).

      However, if I then compile just Bar.java

      $ javac Bar.java

      I get the following errors:



      Bar.java:4: incompatible types; no instance(s) of type variable(s) T exist so that Foo.FooInner<T> conforms to Foo.FooInner<java.lang.Void>
      found : <T>Foo.FooInner<T>
      required: Foo.FooInner<java.lang.Void>
      public Foo.FooInner<Void> i1=foo.getInner();
      ^
      Bar.java:5: incompatible types
      found : Foo.FooInner<java.lang.Long>
      required: Foo.FooInner<java.lang.Long>
      public Foo.FooInner<Long> i2=foo.<Long>getInner();
      ^

            ahe Peter Ahe
            ahe Peter Ahe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: