Static Import Method Overrides

if you have a class with static import in java.lang.Integer , and my class also has a static method parseInt(String) , then which method will call parseInt("12345") ?

Thanks at Advance!

+6
source share
2 answers

If you are inside your class, it will call your method.
If you are outside your class (and import both classes), you must specify which class to use.

Prove: http://java.sun.com/docs/books/jls/download/langspec-3.0.pdf $ 8 and $ 6.3 (see comments)

+6
source

Try the following:

 import static java.lang.Integer.parseInt; public class Test { public static void main(String[] args) { System.out.println(parseInt("12345")); } private static int parseInt(String str) { System.out.println("str"); return 123; } } 

result:

 str 123 

the method in your class is executed first.

+5
source

Source: https://habr.com/ru/post/904891/


All Articles