*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static overloading" Watch "Static overloading" New topic
Author

Static overloading

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Can anyone tell me why the following code gives complier error at line



Shoudn't it call the overloaded version of the static method in the Main1 class. Please anyone!. Thanks in advance.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
print(); //Gives error here.
is equalent to
this.print();
or you can write
Main.print();

Now if you write this.print() or Main.print() then these will not work
because there is no print method present in Main class that takes 0 argument
so this.print() or Main.print() will fail.

but Main1.print() will work.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hello Gowher,

My question is why can't the print() look for in Main1 class instead of Main class. Can the static methods be overloaded? Please answer to the point.

Please also know that the this operator cannot be applied to static methods.

Regards,
Jothi Shankar Kumar. S
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
my quick answer is:

the compiler is only looking static method within a class.

if you change the line to: print("I'm main class"). it should work.

cheers


SCJP 5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Tony,

There is also a static method within Main1 class which matches print() with no args. Why dont the compiler take that method insted of spitting a compile time error?

Regards,
Jothi Shankar Kumar. S
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
Note that Main1 extends Main and not the otherway around. Hence Main will not check in Main1 class.
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
I think there are couple of questions here -
1. Can we overload static method?
2. Why the program does not find print() of the second class?

My Views:
1. method overloading is the primary way polymorphism is implemented in Java. Java uses late binding to support this. Polymorphism makes sense when it comes with the instance of a class (object). This is the reason you can not override static method.

2. I am wondering why do you think compiler should find the print() method of Main1 class. You are calling print method on self (i.e. "Main" class) through static method of the class so compiler will try to find it in the "Main" class.


love your job and not your company;
...because you never know when your company will stop loving you!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Patil,

Thanks for the reply. So the conclusion will be static methods can't be overriden nor be overloaded. They just can be redefined.

Regards,
Jothi Shankar Kumar. S
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Originally posted by joshua antony:
Note that Main1 extends Main and not the otherway around. Hence Main will not check in Main1 class.


Even the otherway around, compiler won't check it.

IMHO, for static method, compiler will only check within one class.

the same happen with overriding, at compile time, compiler doesn't know if it will call a method in superclass or subclass.
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
Even the otherway around, compiler won't check it.

Try this instead - it compiles just fine.



The call to print() is available now because Main1 implements it and Main extends Main1. You can't override it but you can hide it. By adding



to the Main class.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Tim,

Thanks for the reply. I tried your code and now I'm understanding it. The vague logic is, if you are calling a static method which is overloaded, then that method must be available in the current class (i.e., the class from which you are making the static method call) or that particular static metod must be available through inheritance. Anyone corrections on this statement.

Regards,
Jothi Shankar Kumar. S
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
An easy way to remember, static methods are resolved at COMPILE time, whereas instance methods are resolved at RUNTIME. Static methods do not take advantage of polymorphism.

Therefore, the declared reference variable type is the type any static method will be invoked from, and not any subtype/implementing type that is used to instantiate an object.

Although the exam wont be this nice to you, but when programming it's usually best to call static methods by qualifying the Class and not calling them from object instances. i.e ClassName.staticMethod() as opposed to myInstance.staticMethod()
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Andy,

Thanks for the reply. I got the concept now with respect to static overloading and overriding.

Regards,
Jothi Shankar Kumar. S
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static overloading
 
Similar Threads
How to store method return type in primitive
Static Method Inheritance ??
overriding
static method overloading
Overriding static methods.