This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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!
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.
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()