• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling static sleep () method from this instance

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic