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!

MozenRath
  • 9,652
  • 13
  • 61
  • 104

2 Answers2

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)

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
  • can you show a reference indicating it is guaranteed by the standard to be the case? It might be compiler depended... – amit Jan 02 '12 at 12:06
  • waiting for sm1 to reply to amit – MozenRath Jan 02 '12 at 12:14
  • 2
    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)). – Sebastian Hoffmann Jan 02 '12 at 12:22
  • @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. – amit Jan 02 '12 at 12:26
  • 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 me – MozenRath Jan 02 '12 at 12:28
  • 2
    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.` – soulcheck Jan 02 '12 at 12:32
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.

jenaiz
  • 547
  • 2
  • 15
  • 2
    can you show a reference indicating it is guaranteed by the standard to be the case? It might be compiler depended... – amit Jan 02 '12 at 12:05