0

I have problem with templates in Java. I don't know what is wrong. I pasted the code from C# to Java and does not work.

class A<T1, T2>
{
....
}

class A<T1, T2, T3>
{
...
}

What's wrong?

  • Possible duplicate of [How to use Class in Java?](http://stackoverflow.com/questions/462297/how-to-use-classt-in-java) – Vivek Nuna Oct 25 '16 at 09:32
  • First of all, you need to have separate files for each class not like in C#, second you can't have two class with the same name even with different parametrized type – Kamil Banaszczyk Oct 25 '16 at 09:34
  • Java and C# are designed differently - Java classes within a package must be uniquely named, independent of the number of type parameters. – Dave Doknjas Oct 25 '16 at 14:02

2 Answers2

0

You cannot have two outer classes with the same name in the same package. Try renaming one of the classes or put it in a different package. Alternatively putting one class as a nested class of another class would also work, but the question is if they really need to have the same name.

dudel
  • 684
  • 5
  • 15
  • So in Java I can't have two classes with the same name and different ? In C# it works. –  Oct 25 '16 at 09:43
  • Unfortunately not, at least not two outer classes in the same package. – dudel Oct 25 '16 at 10:08
0

The reason for Java not allowing more than one outer class in a package with the same name is that when Java worked generic classes into its feature set, the decision was made to do this via 'type erasure'. The generic type parameter information is replaced at runtime, leaving just ordinary non-generic classes: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

C# & VB.NET also started off without generics, but when they worked generics into their feature sets, the decision was made to implement generics at the byte code level.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28