wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes calling static sleep () method from this instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling static sleep () method from this instance" Watch "calling static sleep () method from this instance" New topic
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: 24183
    
  34

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: 19656
    
  18

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 - OCEEJBD 6
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: 38107
    
  22
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: 862
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: 24183
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling static sleep () method from this instance
 
Similar Threads
Thread.sleep() does not release lock?
override sleep method
thread question in k&b's book for 1.5 (3)
What is wrong here?
How main in superclass is called from subclass