• 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

Examlab Thread question

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am having difficulty understandinng this code.
doSleep() method is a static method. why it is possible to call it from run ( non static method.)
Please explain. Thanks.

Anu

 
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anu Bhagat wrote:Hi,
doSleep() method is a static method. why it is possible to call it from run ( non static method.)



Hi Anu,

Why is it impossible? We can always call static method from non-static context right? But the other way around - calling non-static from static context is not possible!

I think you got confused with static modifier; understand that static members are class properties which are loaded by the JVM when the class is loaded; but instance members are loaded only when an instance is created. So by the time an instance is instantiated, its static members would be in place right? So no problem calling them by static members right?

Hope you are not more confused now
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Anu

One class member is shared among all instances of class.
If class method want to call instance method then class member need to know object reference to call instance method of particular object. Without object reference class method doesn't know which instance method to call.
 
Anu Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I got mixed up and couldn't see it clearly. ok now
Anu
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a follow up question:
How can the doSleep() method pass an int argument, to the sleep() method which takes a long?
-Thank you
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There will be automatic upcast from int to long...
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:There will be automatic upcast from int to long...


Ok, so if it were the other around, a long being sent to a int, it would require a explicit downcast, or else it would fail.
Is that correct?
-Thank you
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's a long sent to an int it requires an explicit downcast otherwise it won't compile.

except when the long is a literal or constant whose value fit inside an int. In such a case the long value is implicit narrowed to an int with
no explicit casting required.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guillaume Jeudy wrote:except when the long is a literal or constant whose value fit inside an int. In such a case the long value is implicit narrowed to an int with
no explicit casting required.



This is not true. An implicit type cast from long to int is never performed



An implicit down cast is performed from int (literal or compile time constant) to short or byte (in range of course) but that too doesn't apply to method calls...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic