jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods invocation ..... 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 methods invocation ....." Watch "Static methods invocation ....." New topic
Author

Static methods invocation .....

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
I am very confused about the invocation of static methods, especially they are overridden in the sub class and accessing with super class reference.
Below i am giving the sample of question, please explain me the logic behind the output. My sincere request is, try to solve the programme without executing the programme and once after getting the solution then execute and compare your results. I am sure you will correctly solve the problem, unlike me ( Before executing the programme i will get one solution and after executing the programme i will get another solution, then i will give some bluff reason for the solution and soon will forget the logic, i am sure you are not like that).
Here is the programme.
--------
class P {
static void printS1(){System.out.println("P.printS1 ");}
void printS1S2(P w){printS1();}
}
public class Q extends P {

static void printS1(){System.out.println("Q.printS1 ");}
void printS1S2(P w){
w.printS1(); // (4)
printS1();}// (5)
public static void main(String[] args) {
new Q().printS1();//(1)
P p = new Q();
p.printS1();//(2)
p.printS1S2(p);// (3)
}}
----
Please tell me if some more complications are existing with static methods.
Thanks in Advance,
Narasimha.


Narasimha
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I usually look at these programs starting from the beginning, so let's look at main():

Line (1) calls Q.printS1() which displays "Q.printS1".
Line (2) calls P.printS1() since p is an instance of the P class. Even though P extends Q, static methods are NOT inherited and don't take part in the polymorphism mechanism. This means the output will be "P.printS1".
Line (3) calls Q.printS1S2() because this method isn't static. Since p really references an object of type Q, polymorphism takes effect.

Line (4) calls P.printS1() since w is a reference to a P object (at least as far as the compiler knows). This results in "P.printS1" being displayed.
Line (5) on the otherhand calls Q.printS1 since the current object is of type Q. This results in "Q.printS1".
Putting this altogether, the output should be
Q.printS1
P.printS1
P.printS1
Q.printS1
I haven't ran the program yet, and since you didn't post the results, I'm not quite 100% sure this is correct. If not, I sure hope someone will correct any errors in my logic.
Layne
[ February 18, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Sandeep Achar
Greenhorn

Joined: Sep 26, 2003
Posts: 18
Hi Narasimha,
Layne is absolutely right. I ran the program and checked the output.
While preparing for SCJP, I made one simple table to remember the concept of static method overriding.
-----------------------------------------------------------------------
Superclass......Subclass........Compliles?......Polymorphism applies?
-----------------------------------------------------------------------
Non-static......Non-static......Yes.............Yes
Non-static......Static............No................-
Static............Non-static......No................-
Static............Static............Yes..............NO
-----------------------------------------------------------------------
Hope the above table helps.
Have a nice day. Bye.
[ February 18, 2004: Message edited by: Sandeep Achar ]

Regards,<br />Sandeep<br />(SCJP 1.4, SCBCD 1.3)
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
Can somebody explain the output of this and why - in light of above discussion?

Thanks
Bijesh
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Bijesh,
Answer will be, Parent -- aMethod, Child -- anotherMethod followed by Child -- aMethod. Below is the explanation,
1. When you say, p1.aMethod() - aMethod() is static, hence it will be invoked from the p1 reference variable's class, which is nothing but Parent class, hence Parent's aMethod() will be invoked and the output is - Parent -- aMethod
2. When you say, p1.anotherMethod() - anotherMethod() is non-static, hence it will be invoked from the run time type of the object, which is nothing but new Child(), hence anotherMethod() from the Child class will be invoked and the output is Child -- anotherMethod.
When you call aMethod() from anotherMethod() from Child class, actually it will be resolved to - this.aMethod(), and this will refer the object on which anotherMethod() is called which is nothing but new Child(), hence aMethod() of Child class will be invoked, hence the output, Child -- aMethod.

I am not sure whether you understood or not( Because of my explanation). You can come up with new set of questions, most welcome.
Bijesh Krishnadas
Ranch Hand

Joined: Aug 08, 2002
Posts: 31
Originally posted by narasimha rao bandlamudi:

2. When you say, p1.anotherMethod() - anotherMethod() is non-static, hence it will be invoked from the run time type of the object, which is nothing but new Child(), hence anotherMethod() from the Child class will be invoked and the output is Child -- anotherMethod.
When you call aMethod() from anotherMethod() from Child class, actually it will be resolved to - this.aMethod(), and this will refer the object on which anotherMethod() is called which is nothing but new Child(), hence aMethod() of Child class will be invoked, hence the output, Child -- aMethod.

In other words within a non-static method (here anotherMethod), the current object(this) will not be considered as the Parent object holding the Child reference, but as a genuine Child object...
Get what I mean??? And if u do, am I right?
Bijesh
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Bijesh, Haven't you read the explanation above my code post here !!! If so what is the part you don't get?


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Originally posted by Bijesh Krishnadas:

the current object(this) will not be considered as the Parent object holding the Child reference, but as a genuine Child object...

Bijesh


Hi Bijesh,

I couldn't get, what exactly you mean in the above sentence?
[ February 19, 2004: Message edited by: narasimha rao bandlamudi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods invocation .....
 
Similar Threads
Are static methods are overriden??
anotherone from Dan's
static hidden methods, and overriden instance methods
Inheritance
Shadowing static methods