1

Hi I have some initialization as below.... it is showing error in eclipse as type mismatch. if i compile using maven through command prompt, it compiles without any error.. using eclipse luna, and compiler set to java 1.7.

  ReportData<Object> rdata = null;

  rdata =  new ReportData<>(reportCtx.get()); // error shown for this line in //eclipse.

  ReportData<T>{

      private T val;
      public ReportData(T val){
          this.val=val;
      }
  }

I am not understanding, why eclipse show error for this and how it compiles in maven with the same java 1.7. whats wrong with rdata = new ReportData<>(reportCtx.get()); initialization.

beresfordt
  • 5,088
  • 10
  • 35
  • 43
Prasanna Ab
  • 116
  • 2
  • 6
  • 2
    And what the error say? – Antoniossss Mar 19 '15 at 13:37
  • error says Type mismatch: cannot convert from ReportData to ReportData – Prasanna Ab Mar 19 '15 at 13:38
  • 1
    You can configure eclipse to display different things as errors warnings or ignore. Window > preferences > Java > Compiler > Errors/warnings. Maybe that is why one works and the other not. – Neilos Mar 19 '15 at 13:39
  • Than how about `rdata = new ReportData(reportCtx.get());` ? – Antoniossss Mar 19 '15 at 13:40
  • What is the signature of the method `reportCtx.get()`? – Duncan Jones Mar 19 '15 at 13:41
  • rdata = new ReportDat(reportCtx.get()) eliminates the error. – Prasanna Ab Mar 20 '15 at 06:18
  • @Neilos the eclipse configuration is default one, and there is nothing configured as error for geneerics section. rdata = new ReportDat(reportCtx.get()) eliminates the error. but i want to know whats wrong with the previous way of initialization and what is the reason for this error. – Prasanna Ab Mar 20 '15 at 06:37
  • @Duncan reportCtx is of type Optional>. signature is T get(); – Prasanna Ab Mar 20 '15 at 06:41
  • The <> operator was, I thought, a bit of a syntax trick to do instantiation on the same line. E.g. `List list = new ArrayList<>()` - if you split it between lines, perhaps the compiler is less happy with the <> as there's less context to work with. – Ashley Frieze Mar 20 '15 at 16:08
  • @AshleyFrieze I tried it with `ReportData rdata = new ReportData<>(reportCTx.get());` on one line. The error is the same. – Gerold Broser Mar 20 '15 at 16:27
  • Intesting - what does reportCTx.get() return? – Ashley Frieze Mar 20 '15 at 17:27
  • @AshleyFrieze `reportCTx.get()` "returns" nothing at compile time. As it was mentioned earlier already it stands for a `T`. But that's not of interest here. It's about `ReportData.` Did you read [my answer](http://stackoverflow.com/a/29170406/1744774)? – Gerold Broser Mar 20 '15 at 18:28

1 Answers1

2

ReportData<?> is the supertype of all kinds of report data.

ReportData<Object> is not the supertype of all kinds of report data.

Hence you cannot assign:

ReportData<Object>ReportData<of an unknown kind>

See The Java™ Tutorials , Generics, Wildcards.

And BTW, in addition to ...

ReportData<Object> rdata = new ReportData<Object>(reportCtx.get());

... the different declaration in ...

ReportData<?> rdata = new ReportData<>(reportCtx.get());

... does the trick, as well.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • I agree with your explaination... but why maven build is not giving error? I am using jdk1.7.0_67 to compile. its not giving any error. it compiles fine. – Prasanna Ab Mar 23 '15 at 06:44