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 Method/Non-Static Method, RTTI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Method/Non-Static Method, RTTI" Watch "Static Method/Non-Static Method, RTTI" New topic
Author

Static Method/Non-Static Method, RTTI

Stacy Ye
Greenhorn

Joined: Feb 11, 2002
Posts: 6
Here's a piece program, Would you mind explain to me, Thanks!
class MyTest {
public void myTest() {
System.out.println("Printing myTest in MyTest class");
}
public static void myStat() {
System.out.println("Printing myStat in MyTest class");
}
}
public class Test extends MyTest {
public void myTest() {
System.out.println("Printing myTest in Test class");
}
public static void myStat() {
System.out.println("Printing myStat in Test class");
}
public static void main ( String args[] ) {
MyTest mt = new Test();
mt.myTest();
mt.myStat();
}
}
I run this one, the answer is:
Printing myTest in Test class followed by Printing myStat in MyTest class
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
Stacy , myTest() method is a non-static method overidden in subclass Test , hence myTest() will print output from the Test class.
myStat() is a static method declared in myTest. Static method cannot be overidden in subclasses hence will print the output from the superclass myStat() method in MyTest class.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The key here is that dynamic lookup is not used when invoking a static method. The first line of your code calls mt.myTest(), which is an instance method. Therefore, at run-time, the JVM determines the run-time type of the object referenced by mt, which is type Test. Therefore, the myTest method in class Test is executed.
However, when you invoke mt.myStat(), you're invoking a static method. Dynamic lookup is not used in this case. Rather, the JVM uses the compile-time type of the variable mt to determine which method to call. The compile-time type of mt is MyTest. Therefore, the myStat method is myTest is invoked.
I hope that helps,
Corey


SCJP Tipline, etc.
Stacy Ye
Greenhorn

Joined: Feb 11, 2002
Posts: 6
if static method can not overriden by subclass, i think it should give me a "compile error".
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
Stacy, re-declaring a static method in a sub-
class doesn't necessarily mean it is being over-
ridden. Static methods are implicitly final.
Argument is analogous to private methods. We
can rightfully re-declare them in sub-classes
but dynmaic method look-up would not be appli-
cable. Perfectly ok with the compiler.
Stacy Ye
Greenhorn

Joined: Feb 11, 2002
Posts: 6
it makes more sense to me now. Thank you very much for the help.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Jay,
re-declaring a static method in a sub-
class doesn't necessarily mean it is being over-
ridden. Static methods are implicitly final.

Wrong! Static methods are not implicitely final. You can still declare a static method with the same signature in a subclass and this is called hiding. But if you declare your static method final in the superclass, then you will not be allowed to hide it in your subclass.
Hiding is to static methods what overriding is to non-static methods.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Stacy Ye
Greenhorn

Joined: Feb 11, 2002
Posts: 6
so it means, the above program with 2 static methods, looks like override method, it is "Hiding". it doesn't have a compile error but the subclass one never used?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
static methods invocation is resolved at compile time and is based on the declared type, that is MyTest. Non-static methods are resolved at runtime by dynamic lookup and is based on the runtime type, that is Test (the one that follows the keyword "new").
Stacy Ye
Greenhorn

Joined: Feb 11, 2002
Posts: 6
thanks Valentin, finally i think i got it!
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
Val,
I still insist on static methods being
implicitly final.
'final' in the context of a method means that
the method can't be overridden. Static methods
can't be overridden, hence they are implicitly
final.
B.T.W. Saying 'implicitly final' doesn't mean
that the keyword final is implicitly included
in the declaration of the method.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Jay,
I'm sorry but this is not true at all. A method (static or not) declared final cannot be redeclared in a subclass.
Check this out:

It doesn't compile because method is declared final in the superclass A.
If you remove final, then it works fine:

From JLS 8.4.3.3 final Methods

A method can be declared final to prevent subclasses from overriding or hiding it. It is a compile-time error to attempt to override or hide a final method

Moreover, the JLS say nothing about static methods being implicitely final.
[ March 28, 2002: Message edited by: Valentin Crettaz ]
Stacy Ye
Greenhorn

Joined: Feb 11, 2002
Posts: 6
Is there any case in the real project need to use this?
I am a beginner and some example doesn't seem to be used in the real work. am i wrong. pls, don't mad if my question sounds like silly.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
your question is not silly at all, but if the feature exists it does mean that it may be useful sometimes. Personally, I have had to use that in several projects...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static Method/Non-Static Method, RTTI