static imports method overlap
Asked Answered
A

2

6

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!

Ampoule answered 2/1, 2012 at 11:51 Comment(0)
R
6

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)

Reunion answered 2/1, 2012 at 11:53 Comment(6)
can you show a reference indicating it is guaranteed by the standard to be the case? It might be compiler depended...Wrac
waiting for sm1 to reply to amitAmpoule
From Java Language Specification $8: The scope (§6.3) of a member (§8.2) is the entire body of the declaration of the class to which the member belongs. | From $6.3: The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name (provided it isvvisible (§6.3.1)).Reunion
@Paranaix: Please add link to the specs and add it to your answer, and you get my +1. It is a full proven answer, in my opinion.Wrac
i think the same, but to accept it i will need the link and these details in the answer. sorry but this is for the good of other who might have the same curiosity as meAmpoule
I think it still misses one point (from jls): §6.3.1 A declaration d of a method named n shadows the declarations of any other methods named n that are in an enclosing scope at the point where d occurs throughout the scope of d.Magnetoelectricity
D
5

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.

Downandout answered 2/1, 2012 at 12:4 Comment(1)
can you show a reference indicating it is guaranteed by the standard to be the case? It might be compiler depended...Wrac

© 2022 - 2024 — McMap. All rights reserved.