• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Static overloading

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that Main1 extends Main and not the otherway around. Hence Main will not check in Main1 class.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,

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

Regards,
Jothi Shankar Kumar. S
 
reply
    Bookmark Topic Watch Topic
  • New Topic