• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generic interface and static generic method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I'm studying for Java exam and I saw in a book that we can have a generic interface and a static generic method as well. I tried to code it in order to learn more about that. See my code:




This code compiles, but when I try to replace the concatenarTextos method by this one bellow it throws an exception:



this exception is thrown:

java.lang.ClassFormatError: Duplicate field name&signature in class file certificacao/generics/ImplementacaoGenerica

why does it occurs?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has "type erasure", which means that generic information doesn't exist at runtime. It's only used by the compiler. So the following signatures:
both compile to the signature:
which means you get a conflict.
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but I'm not trying to use both at the same time like this:


I'm trying to compile only this method bellow:


Here is all the code:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bruno Sant Ana wrote: ... but when I try to replace the concatenarTextos method by this one bellow it throws an exception:



this exception is thrown:

java.lang.ClassFormatError: Duplicate field name&signature in class file certificacao/generics/ImplementacaoGenerica

why does it occurs?



Well, both "T" and "U" can be String objects (or can be Object objects -- the super of String). Question: assuming that they are *not* string objects, then how should the compiler perform the "t+u" operation?

Henry
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but even if I change to:



it doesn't compile. Why?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bruno Sant Ana wrote:but even if I change to:



it doesn't compile. Why?



Hint: what happens when you cast it (the parameter to the println() method) to an Object type?

Henry
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bruno Sant Ana wrote:OK, but I'm not trying to use both at the same time...


Ah, sorry, I misunderstood.

The reason you get an error (and I get a different error to the one you reported) is that super isn't allowed as a bound on a type variable. You can have lower bounds on wildcard type arguments, but not where you've used them.

The relevant parts of the Java Language Specification are http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4 and http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.5.1. Look at the definition for a type variable - it doesn't mention super. As to why there's a difference? Well, there's a reference to that in the following line:

Unlike ordinary type variables declared in a method signature, no type inference is required when using a wildcard. Consequently, it is permissible to declare lower bounds on a wildcard, using the following syntax, where B is a lower bound:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic