This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Method/Non-Static Method, RTTI

 
Stacy Ye
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stacy Ye
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if static method can not overriden by subclass, i think it should give me a "compile error".
 
J Hreich
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it makes more sense to me now. Thank you very much for the help.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stacy Ye
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Valentin, finally i think i got it!
 
J Hreich
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic