Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static imports method overlap

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

Thanks in Advance!

like image 913
MozenRath Avatar asked Jan 02 '12 11:01

MozenRath


2 Answers

If you're inside your own class it will call your method.
If you're 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)

like image 116
Sebastian Hoffmann Avatar answered Nov 09 '22 13:11

Sebastian Hoffmann


Try this:

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;
    }
}

the result:

str
123

the method in you class is executed first.

like image 25
jenaiz Avatar answered Nov 09 '22 12:11

jenaiz