• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Name clash: Two methods have the same erasure  RSS feed

 
Ranch Hand
Posts: 56
7
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two methods that I want to overload:





The compiler error I encounter:



If I rename the one method, the code compiles, but I want to know why this is considered a name clash.

Why is the code above not an example of method overloading where the data type of the parameters is different?

How do I fix this, so that I can overload these methods?
 
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't overloading. They both have the signature saveList(List). Remember that generic information is removed at runtime, so the compiler cannot distinguish those two methods.
I am not sure what the correct answer is because you haven't provided enough information about the two methods. I am a bit suspicious, however, that you are acting outwith the List's original owning object, which means you are breaching the single responsibility principle. I have found a few places where you can find out about that: 1 2 and enclosed links. If there is an object with those Lists in, maybe that object should be responsible for maintaining those Lists. Unless you have a generic list saving utility, consider moving those methods elsewhere.
 
Sheriff
Posts: 12966
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because of type erasure: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

And I think you might have gotten it backwards there, Campbell. Generic types are used by the compiler to provide tighter type checking. Type erasure will replace the generic types with ordinary types (classes, interfaces) and methods, typecasts to preserve type safety, or bridge methods to preserve polymorphism. It's the runtime that has no idea of generics, so the error is the compiler basically saying: "Oops, when I apply type erasure these two methods turn out to have the same exact signature. That's not going to work for the JVM."
 
Jay Rex
Ranch Hand
Posts: 56
7
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell and Junilu.

Note to self: Use the search function before posting, as this has been covered already
 
Campbell Ritchie
Marshal
Posts: 62819
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jay Rex wrote:Thanks Campbell and Junilu. . . .

That's a pleasure And sorry that I worded my reply so badly.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!