aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic interface and static generic method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic interface and static generic method" Watch "generic interface and static generic method" New topic
Author

generic interface and static generic method

Bruno Sant Ana
Greenhorn

Joined: May 17, 2012
Posts: 29
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?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

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

Joined: May 17, 2012
Posts: 29
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:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18965
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bruno Sant Ana
Greenhorn

Joined: May 17, 2012
Posts: 29
but even if I change to:



it doesn't compile. Why?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18965
    
  40

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

Joined: Apr 06, 2010
Posts: 4457
    
    8

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generic interface and static generic method