| Author |
calling static sleep () method from this instance
|
Rahul Shilpakar
Ranch Hand
Joined: Aug 29, 2006
Posts: 132
|
|
Hi, as i know we can not invoke static method from this instance. In the follwoing code its not giving me any comiplation error and programm just works fine. In below at LINE 1 i called sleep method of Thread on this which is not conceptually correct but still it works. I wonder hows this possible
|
Perform for today. Adapt for tomorrow.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
This is not a thread-related issue; the following applies for all static methods. You can call a static method on an instance of its declaring class; the compiler ignores the instance and generates the same bytecode as if you had invoked the method using the name of the class. The instance is not used at runtime, and in fact, the variable can be null and the method will still be called!
|
[Jess in Action][AskingGoodQuestions]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Previous versions of Java even allowed invalid indexes: I don't know at which version it started (I think 1.4), but the array element is evaluated first now. If that returns null that isn't a problem because, as Ernest said, it's the reference type that counts.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rahul Shilpakar
Ranch Hand
Joined: Aug 29, 2006
Posts: 132
|
|
ok, then we can invoke static mehtods with class instance. and compiler suggests us "You should invoke static methods in static way". Will there be any significant difference in performance or memory? what will be difference when i say
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
You have already been told there would be no difference in performance, since it is identical bytecode. It is a case of style and maintenance of the code; people coming back to your code next year may handle it differently if they know it's a static method.
|
 |
Brian Cole
Author
Ranch Hand
Joined: Sep 20, 2005
Posts: 852
|
|
Originally posted by Rob Prime : The instance is not used at runtime, and in fact, the variable can be null and the method will still be called!
Originally posted by Ernest Friedman-Hill: I don't know at which version it started (I think 1.4), but the array element is evaluated first now. If that returns null that isn't a problem because, as Ernest said, it's the reference type that counts.
I haven't tested this, but I believe this "bug" was fixed at some point and calling foo.staticMethod() will now throw a NullPointerException if foo evaluates to null. [edit: No it won't. Check out 15.12.4.6 of the JLS and disregard what I wrote below.] I think it's good that the implementation agrees with the JLS, but in this case I think it may have been better to update the JLS to allow null rather than to change the compiler to generate code to perform an otherwise unneeded evaluation. (Then again, it's best just to not invoke static methods in this manner, which makes the point moot.) [edit: mea culpa. I do remember reading this somewhere, but it was obviously in error.] [ November 23, 2008: Message edited by: Brian Cole ]
|
bitguru blog
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Originally posted by Brian Cole: [edit: mea culpa. I do remember reading this somewhere, but it was obviously in error.]
Yes, I remember reading about that too. It might have been an experimental thing that they did an about-face on.
|
 |
 |
|
|
subject: calling static sleep () method from this instance
|
|
|