Forums Register Login

static and instance methods

+Pie Number of slices to send: Send
The static method declared in class cannot hide the instance method of the same signature declared in its super class.
The instance method declared in class cannot override the static method of the same signature declared in its superclass.
Whats the reason behind it?
+Pie Number of slices to send: Send
1) Static methods are resolved at compile time, instance methods are "late binding" which means that they are not resolved until runtime, and at runtime the JVM decides between the available methods which one is appropriate.
2) Static methods are always invoked without reference to a particular instance, and instance methods are always invoked with reference to a particular instance.
3) Static methods get loaded to the method area at class load time, instance methods get loaded to the method area at the last possible minute before execution.
Since in either case you describe, one of the methods is static, there is no way for late binding to occur, because the static one is not available as a choice anymore (it's already in place). The JVM just can not handle that situation, so instead the compiler handles it by causing a compile time error.
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 895 times.
Similar Threads
hiding instance method
Modifers are so much fun! So are static methods!
Overriding doubt ?
hide vs override
static methods
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:24:47.