• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method overridden!! need felp!!

 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Adi!!
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi adi,
Excellent explanation! thank you
can any body explain where the methods will store??
 
Adi Kulkarni
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic