Search code examples
javaclassloaderoverloading

Will library overloaded method be used without recompiling?


Say there was a method in the library

public static <E> void doSmth(Collection<E> foo, Collection<E> bar){...}

and it was used in the following code:

Set<Object> foo = ...;
List<Object> bar = ...;
doSmth(foo, bar);

Now another version of library substitues the former in the classpath and it has two methods:

public static <E> void doSmth(Collection<E> foo, Collecion<E> bar){...}
public static <E> void doSmth(Set<E> foo, List<E> bar){...}

The application is not recompiled and run. Which method will be used?


Solution

  • Overload resolution is a compile-time process, therefore its result can't change without recompilation.

    So,

    public static <E> void doSmth(Collection<E> foo, Collecion<E> bar){...}
    

    will be used, since its signature is specified in the compiled file.