Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling static sleep () method from this instance

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic