Results 1 to 3 of 3
- 11-11-2008, 10:57 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 8
- Rep Power
- 0
why we cann't override static methods
1 class A{
2 static void go() {
3 System.out.println("red");
4 }
5 }
6 class B extends A {
7 static void go () {
8 System.out.println("green");
9 }
10 public static void main(String args[]) {
11 go(); //outputs green
12
13 A a=new B();
14 a.go();//outputs red
15 }
from line 11 it seems method is overridden
but from 14 , we get to know its not overridden
why is the difference? why cann't we override the static methods? Is there any use of static method? Is there any advantage of static method over instance method
- 11-11-2008, 11:05 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Because static methods only define in once at class load.
- 11-11-2008, 11:07 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
How To Override Methods, NetBeans 6.1
By markw8500 in forum NetBeansReplies: 1Last Post: 08-16-2008, 11:58 PM -
Override Methods In NetBeans IDE
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks