• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Examlab Thread question

 
Anu Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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

 
Jisha Anand
Ranch Hand
Posts: 62
Flex Java
  • Mark post as helpful
  • send pies
  • 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
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks. I got mixed up and couldn't see it clearly. ok now
Anu
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Guillaume Jeudy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic