Does the method parameters with different generics make the methods have different signature?

This works when the method can be resolved at compile time because the return type is part of the signature. After erasure you have two methods public EJBHome lookupHome(Class homeClass) throws PayrollException; public EJBLocalHome lookupHome(Class homeClass) throws PayrollException You cannot define these without generics but as the return type is part of the signature, these are different methods You can call lookupHome(EJBHome. Class); lookupHome(EJBLocalHome.

Class) but not Class c= EJBHome. Class; lookupHome(c); // Ambiguous method call EDIT: Try the following for (Method method : EJBHomeFactory.class. GetDeclaredMethods()) { System.out.

Println(method); } And you should see something like public javax.ejb. EJBHome EJBHomeFactory. LookupHome(java.lang.

Class) public javax.ejb. EJBLocalHome EJBHomeFactorylookupHome(java.lang. Class) Similarly, if you use javap -c invokevirtual #8; //Method lookupHome:(Ljava/lang/Class;)Ljavax/ejb/EJBHome invokevirtual #10; //Method lookupHome:(Ljava/lang/Class;)Ljavax/ejb/EJBLocalHome EDIT If you want an example of where the return type is part of the signature class A { public static Byte getNum() { return 0; } } class B { public static void main(String ... args) { int I = A.getNum(); System.out.

Println(i); } } Compile and run this and you get no error, now change the signature of getNum in A to class A { public static Integer getNum() { return 0; } } and compile only the class A. If the return type was not part of the signature, this would make no difference to B, however if you run B without recompiling it you get Exception in thread "main" java.lang. NoSuchMethodError: A.getNum()Ljava/lang/Byte; at B.

Main(B. Java:10) As you can see, the return type Ljava/lang/Byte is the inetrnal name of the return type and part of the signature.

This works when the method can be resolved at compile time because the return type is part of the signature. After erasure you have two methods public EJBHome lookupHome(Class homeClass) throws PayrollException; public EJBLocalHome lookupHome(Class homeClass) throws PayrollException; You cannot define these without generics but as the return type is part of the signature, these are different methods. You can call lookupHome(EJBHome.

Class); lookupHome(EJBLocalHome. Class); but not Class c= EJBHome. Class; lookupHome(c); // Ambiguous method call.

EDIT: Try the following. For (Method method : EJBHomeFactory.class. GetDeclaredMethods()) { System.out.

Println(method); } And you should see something like public javax.ejb. EJBHome EJBHomeFactory. LookupHome(java.lang.

Class) public javax.ejb. EJBLocalHome EJBHomeFactorylookupHome(java.lang. Class) Similarly, if you use javap -c.

Invokevirtual #8; //Method lookupHome:(Ljava/lang/Class;)Ljavax/ejb/EJBHome; invokevirtual #10; //Method lookupHome:(Ljava/lang/Class;)Ljavax/ejb/EJBLocalHome; EDIT If you want an example of where the return type is part of the signature.. class A { public static Byte getNum() { return 0; } } class B { public static void main(String ... args) { int I = A.getNum(); System.out. Println(i); } } Compile and run this and you get no error, now change the signature of getNum in A to class A { public static Integer getNum() { return 0; } } and compile only the class A. If the return type was not part of the signature, this would make no difference to B, however if you run B without recompiling it you get Exception in thread "main" java.lang.

NoSuchMethodError: A.getNum()Ljava/lang/Byte; at B. Main(B. Java:10) As you can see, the return type Ljava/lang/Byte; is the inetrnal name of the return type and part of the signature.

Peter, thanks for your answer. Does it mean whether the compiler throws errors depends on how these two methods are called? Or in a extreme case, if these two methods are never called, the compiler will throw errors on these two methods?

– Sapience Feb 24 at 19:22 The return type is not part of the signature. – jprete Feb 25 at 6:53 @jprete, the return type is part of the signature at the byte code level as I have demonstrated. How would you support your statement?

– Peter Lawrey Feb 25 at 9:09 The compiler only throws errors which it crashes due to a bug in the compiler, it usually reports errors in your code. It will report an error if it thinks the call is ambiguous. If the two methdo are never called it will not report an error if it would compile with a call.

– Peter Lawrey Feb 25 at 9:11 The Java language specification explicitly states that return types are not part of the signature of a method. I could not be sure whether the specification makes the two cited methods are legal or not. My intuition said no, but I do not think the specification explicitly disallows this case because they are distinguishable at compile time in the same way as two methods with the same name.

Cases similar to this are disallowed, but not necessarily this one. – jprete Feb 25 at 23:38.

– Peter Lawrey Feb 25 at 9:11 Bruce Eckel "Thinking in Java", Chapter 5 - Method overloading, also google thinks the same download.oracle. Com/javase/tutorial/java/javaOO/methods. Html – Lev Khomich Feb 25 at 13:14.

With generics, the two methods do have different argument types, so compiler should be able to separate them. Checking the current language spec, the two methods should be allowed. I heard that in Java 7, they will not be allowed, which is pretty bad in theoretical sense.To answer your question, yes, they do have different signatures, based on argument types.

But practically, you should avoid such confusion anyway. Have two different method names. Public T lookupHome(Class homeClass) public T lookupLocalHome(Class localHomeClass) Overloading is never necessary.

When in doubt, break overloading by using different names.

You are right, using different names would be better. However, I was not the author of that existing code, and I just don't know why Eclipse always complains the same erasure. I tried to compile by using javac, and it failed too.

– Sapience Feb 25 at 14:22.

These are duplicate methods. Type erasure means that Class is reduced to Class at runtime, which means that you have two methods that each have the signature lookupHome(Class homeClass). My understanding is that the compiler should not compile this code; if you have a version of javac that compiles it, then something is wrong!

Your best bet is to rename both methods so that they each have a more specific name than lookupHome. EDIT: After poking around in the language spec for a while, I think it is very possible that the declaration is legal, although I'm not 100% sure. I still think it's a bad idea to have two methods with identical signatures after erasure.

The sample code does compile in our project. The project was compiled using dozen of ant scripts, and I haven't looked deep into how it works. But it seems the compiler is org.eclipse.jdt.core.

JDTCompilerAdapter – Sapience Feb 24 at 19:41 it compiles it and it works because they really do have different sugnatures, have a look at the byte code with javap -c – Peter Lawrey Feb 25 at 9:12.

Oracle and JDBC performance: INSERT ALL vs preparedStatement.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions