This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method overridden!! need felp!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static method overridden!! need felp!!" Watch "Static method overridden!! need felp!!" New topic
Author

Static method overridden!! need felp!!

himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
class A {
static void method( ) {
System.out.print("It's me A . ");
}
}

class B extends A {
static void method( ) {
System.out.print("It's me B . ");
}
}

class Test {
public static void main(String[ ] args) {
A x = new A( );
x . method( );
A y = new B( );
y . method( );
}
}


OUTPUT IS:
Its me A. Its me A

my confusion:

1. How a static method is getting overridden(in k&b its a property that static methods cant be overridden),TELL also when static methods can be overridden.

2.in case of overriding DECISON of method to choose is RUNTIME.SO, y.method(); should call class B's method();

...........
.........
plesse help me guys!!


SCJP 5
swapnil dangore
Ranch Hand

Joined: Jun 05, 2006
Posts: 46
Static method can be re-defined and not overriden..This is redefination of method...Only instance method can be overriden...Method() is called twice because reference type is class A


Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
thanks swapnil,
but one thing that i am not getting is that at runtime the object will be of B so it should call B's method.
is is decided at compile time?
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
As the previous poster told the static methods cannot be overridden , but can be redefined in the child class . In your case the method has been redefined.
In the first case : A x = new A() ; x.method(); ... dont get confused by the instance x . One thing to remember is that static methods belong to the class and not to any specific instance. x.method() will be replaced by the ClassName.method() i,e A.method.

In the second case A y = new B() where B extends A . Again you have called the method y.method(). Dont get confused by the instance y since the method you are calling is a static method. It will replace y by class name i.e A.method() again.
If you want the method in class B to run then you have to call it as B.method();

Remember : Static methods dont belong to any instance as such , they belong to the class. So dont be confused even if they are called using reference to any class. The reference will be replaced by the respective class name.

~Aditya
SCJP 1.5


SCJP 1.5
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
thank you Adi!!
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
Hi adi,
Excellent explanation! thank you
can any body explain where the methods will store??


I know Life is very Tough...But I AM TOUGHER
SCJP 1.5
Adi Kulkarni
Ranch Hand

Joined: Mar 12, 2009
Posts: 86
Hi Nagaraj,

Please refer to this thread. It will tell you what is stored on stack and what on heap.

http://www.coderanch.com/t/416620/Java-General-beginner/java/What-stored-Stack-Heap

~Aditya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method overridden!! need felp!!
 
Similar Threads
Static methods : Difference between redefining and overriding ?
Good Q's from JWhiz
static methods-overridden
I'm so confused, pls help
Covariant returns