Accepted answer

there is only one method, not two, and it is static. while you can call a static method via an instance reference, it's not good style. it indicates the programmer thinks he or she is calling an instance method. a confused programmer might be thinking he or she can cause another thread (not the current one) to sleep this way, when that's not what it does.

both your lines of code do the same thing but the second is better style.


thread.currentthread().sleep(x); or the way eclipse says so thread.sleep(x); static context is required if it is in the need, so we hold on for slight delay with this sleep.

static paradigm set by one object, only affects that particular object heap print life cycle, again considering the overall object life cycle static is not that bothersome, if required it can be used to ease the coding, but to be done carefully as static foot-print is referred by class (for example:- class.forname(pkg.classname)) as like by name and not by any object which is run-time single print copy of class in heap memory.

again usage of object also has pros & cons by weak, phantom, strong kind of references....,

code is convoluted by nature. it is just the way how we do in order to make it work & functional.


the two method calls are identical in behavior because they invoke the same method but using the class name (thread in this case) rather than instance for accessing static fields and methods makes this static-ness clear. that is why this warning is produced.

but considering that static fields and methods are shown in a particular way in most ides (for example in italic font in eclipse and intellij idea), is this warning still necessary? maybe not as much necessary as it was in the early days of java that simple editors were in use.


in java, sleep is a static method. both your examples do exactly the same thing, but the former version is confusing because it looks like it is calling a method on a specific object but it's not doing that at all. in your example it won't matter much, but it is more dangerous if you have the following:


this time it looks like you are telling some other thread to sleep, but in fact you are putting the current thread to sleep. the way to avoid making this type of mistake is to always call static methods using the class rather than a specific object.

Related Query

More Query from same tag