This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Examlab Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Examlab Thread question" Watch "Examlab Thread question" New topic
Author

Examlab Thread question

Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
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



SCJP5.0, SCJA
Jisha Anand
Ranch Hand

Joined: Apr 02, 2009
Posts: 62

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


SCJP, SCWCD, SCBCD
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
Thanks. I got mixed up and couldn't see it clearly. ok now
Anu
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
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


SCJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

There will be automatic upcast from int to long...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
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

Joined: Jul 27, 2009
Posts: 24
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.


SCJP 1.4 and 6.0, SCJD
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...
 
jQuery in Action, 2nd edition
 
subject: Examlab Thread question
 
Similar Threads
Thread doubt (examlab question)
why Compile time error when trying to us static with Synchronized ?
Threads again
Doubt on a Devaka Cooray ExamLab practice question
Examlab(SCJP 6 Practice exam 3) Thread Question