1

I want to get proper understanding why below compilation error? As per my understanding If i use Test.xyz() then compiler look for only static method not for instance method then why below compilation fail?

class Test {
    public static void main(String arg[]) {
        Test.xyz(10);     // compilation fail
    }   

    public void xyz(int i) {
    }
    public static void xyz(Integer i) {
    }   
 }

Every one please suggest why compilation fail rather than other suggestions and how to use , I know all basic thing Autoboxing etc .

This question is asked already by me Which method is looked first by Compiler , Static or instance method when ClassName.method() is used? but in that question most of people go in wrong direction due to mistake in my question so i moved that question to new question.

Community
  • 1
  • 1
Kamlesh Kanazariya
  • 1,209
  • 2
  • 15
  • 32
  • Is this the good process? why ask again the same question? delete the first question if you want to reformulate and start again. – T.Gounelle Mar 28 '15 at 11:35
  • You got the correct answer already, when you asked previously. It makes no sense to ask the same question again, in the hope of something different. – Dawood ibn Kareem Mar 28 '15 at 11:37
  • @DavidWallace I can't get proper understanding from previous question and all person go to wrong direction due to mistake in my previous question – Kamlesh Kanazariya Mar 28 '15 at 11:41
  • 1
    Well, the correct answer is there, in the previous question. Please don't fill up Stack Overflow with repeats of the same question. – Dawood ibn Kareem Mar 28 '15 at 11:42

1 Answers1

2

You are wrong in your assumption: the compiler will first look for most specific candidate methods with the signature (method name+parameters), without boxing. It means that the selected method is the non static one.

Then in a second step the compiler will see that you call the method as a static one with the syntax <Class>.<method>().

It's why you get an error.

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32