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 is how I did the static class.
Ok, So here comes the concept of
autoboxing
in Java.U wrote:
But since you are invoking the
xyz
method directly via class name, clearly means you want to access thepublic static xyz(Integer)
method of the classTest
.But what happens in the compilation process is, first your
javac
compiler checks for method signature that is to be accessed, and after that, it checks for it's access(public
,private
,protected
,default
) and non-access(final
,static
,etc
) modifiers.The same thing happened with this code.
The first thing Java did was, it checked for the existence of the method with the signature
xyz(int)
and notxyz(Integer)
coz you've passed in10
and notnew Integer(10)
as a parameter.It found two methods,
1.
xyz(int)
2.
xyz(Integer)
Had
xyz(int)
not existed, it would have applied the concept of autoboxing(ie, automatically convert10
tonew Integer(10)
) and selectedxyz(Integer)
to be executed. But sincexyz(int)
exists, it does not autobox10
tonew Integer(10)
and selectsxyz(int)
instead ofxyz(Integer)
.Now since your compiler has selected
xyz(int)
to be executed, it checks for it's non-access modifiers. Now since the methodxyz(int)
is non-static, you are expected to access it with an object ofTest
class as follows:And still, if you want to access the
static xyz(Integer)
method, you may have to use:Hope this helps.
You have no return type here:
This should be void, if there is nothing to return:
And also, you need to make the first method static too:
So it can be called out of the static main method. It is not possible to call non-static methods out of static methods. More detailed explanation on this: calling non-static method in static method in Java
Why there is a compilation error
Compilation proceeds through different steps. Extracted from the JLS, following are the rules that explain why you got this error.
I skip the first step which is not relevant in your case. Everything happens in the same class.
Second step: Determine method signature
From above comments, the method you invoke with
Test.xyz(10);
is the one that takes anint
parameter:But now, there is a third step: Choosing the appropriate method
Again, from the above comment, you invoke the method in a
static
form,but unfortunately, the method chosen from the second step, is not static.
It's why an IDE like Eclipse will suggest to "Change 'xyz()' to static".
But as explained in my first answer (deleted), you can either call
public void xyz(int i) {}
on an instance of classTest
, or call thestatic
method with anInteger
parameter:Test.xyz(Integer.valueOf(10));
.Both will work.