File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static methods 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" Watch "static methods" New topic
Author

static methods

wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
B) Static methods can be overridden by static methods only.
is it right?
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
no. static methods are not overriden. they are hidden by the child class.
e.g.
class a {
static void m() {
S.o.p("in a.m()");
}
}
class b extends a {
static void m() {
S.o.p("in b.m()");
}
void n() { m(); }
}
class test {
static void main(String[] s) {

b b1 = new b();
b1.n(); // result is "in b.m()" and not "in a.m()"
// still u can access a's m() thru a.m() or
// super.m() in class b
}
}
regards
maulin
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
yes you are right that static methods can only override static method.
Put simply, a non-static method in a subclass cannot override a static method in a superclass.
the compiler shall give you a error message saying that this is illegal.
Originally posted by wei liu:
B) Static methods can be overridden by static methods only.
is it right?

power jxz
Greenhorn

Joined: Dec 27, 2001
Posts: 3
Static methods may not be overridden to be non-static.
Non-static methods may not be overridden to be static.
Actually, static methods are not participating in the usual overriding mechanism of invoking the
methods based on the class of the object at runtime. Static method binding is done at compile time, so
the method to be invoked is determined by the type of reference variable rather than the actual type of
the object it holds at runtime.
public class StaticOverridingTest {
public static void main(String s[]) {
Child c = new Child();
c.doStuff(); // This will invoke Child.doStuff()
Parent p = new Parent();
p.doStuff(); // This will invoke Parent.doStuff()
p = c;
p.doStuff(); // This will invoke Parent.doStuff(), rather than Child.doStuff()
}
}
class Parent {
static int x = 100;
public static void doStuff() {
System.out.println("In Parent..doStuff");
System.out.println(x);
}
}
class Child extends Parent {
static int x = 200;
public static void doStuff() {
System.out.println("In Child..doStuff");
System.out.println(x);
}
}
above from Chairyuan_note!!!
wei liu
Ranch Hand

Joined: Dec 06, 2001
Posts: 56
so the above statement is correct! right?
Originally posted by power jxz:
Static methods may not be overridden to be non-static.
Non-static methods may not be overridden to be static.
Actually, static methods are not participating in the usual overriding mechanism of invoking the
methods based on the class of the object at runtime. Static method binding is done at compile time, so
the method to be invoked is determined by the type of reference variable rather than the actual type of
the object it holds at runtime.
public class StaticOverridingTest {
public static void main(String s[]) {
Child c = new Child();
c.doStuff(); // This will invoke Child.doStuff()
Parent p = new Parent();
p.doStuff(); // This will invoke Parent.doStuff()
p = c;
p.doStuff(); // This will invoke Parent.doStuff(), rather than Child.doStuff()
}
}
class Parent {
static int x = 100;
public static void doStuff() {
System.out.println("In Parent..doStuff");
System.out.println(x);
}
}
class Child extends Parent {
static int x = 200;
public static void doStuff() {
System.out.println("In Child..doStuff");
System.out.println(x);
}
}
above from Chairyuan_note!!!

power jxz
Greenhorn

Joined: Dec 27, 2001
Posts: 3
yes! yours is correct!
Originally posted by wei liu:
so the above statement is correct! right?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
"Static methods may not be overridden to be non-static.
Non-static methods may not be overridden to be static."
can the list please confirm that what this means is that a static method can override another static method. And a non-static method can override non-static method.
overriding amongst static and non-static does not make sense or is illegal or basically not permitted by the language rules.
thanks
mark
Originally posted by power jxz:
Static methods may not be overridden to be non-static.
Non-static methods may not be overridden to be static.
Actually, static methods are not participating in the usual overriding mechanism of invoking the
methods based on the class of the object at runtime. Static method binding is done at compile time, so
the method to be invoked is determined by the type of reference variable rather than the actual type of
the object it holds at runtime.
public class StaticOverridingTest {
public static void main(String s[]) {
Child c = new Child();
c.doStuff(); // This will invoke Child.doStuff()
Parent p = new Parent();
p.doStuff(); // This will invoke Parent.doStuff()
p = c;
p.doStuff(); // This will invoke Parent.doStuff(), rather than Child.doStuff()
}
}
class Parent {
static int x = 100;
public static void doStuff() {
System.out.println("In Parent..doStuff");
System.out.println(x);
}
}
class Child extends Parent {
static int x = 200;
public static void doStuff() {
System.out.println("In Child..doStuff");
System.out.println(x);
}
}
above from Chairyuan_note!!!

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys,
'static' methods CANNOT BE OVERRIDDEN! They belong to the class; overriding only applies to instances of a class or objects.

8.4.6.2 Hiding (by Class Methods)
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the
same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the
class. A compile-time error occurs if a static method hides an instance method.

Please see this Sun Tech Tip which explains shadowing, hiding and overriding.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport
[This message has been edited by Jane Griscti (edited December 29, 2001).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods