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
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.
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- How to print Java object's current value to console, instead of suspend thread while debug in the Eclipse?
- Java Thread catching all Exceptions?
- Getting incorrect output in java thread application?
- Java automatically waits for thread to finish before starting a new one
- Java Exception in thread main - ClassNotFoundException
- Exception in thread "something thread" java.lang.OutOfMemoryError: Java heap space. What can I do?
- Detailed java thread state in eclipse debugger?
- Invalid Thread Access error when changing Java SWT Label
- Create a new console for each thread in java
- Exception in thread "main" java.lang.NullPointerException error - Eclipse Java linked lists
- Java thread ending with a delay
- Java thread with Scanner works first but not second time?
- 16 Thread / 16 core code profiling with Java / Eclipse TPTP on Linux system
- Java process inputstream in thread
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space on eclipse
- Java Thread program not working
- Eclipse Java - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
- Error launching Chrome with Selenium WebDriver in Java Eclipse -Exception in thread "main"
- Exception in thread "JavaFX Application Thread" java.lang.OutOfMemoryError: Java heap space
- Exception in Java Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- Java Throws exception in thread main java.lang.NoClassDefFoundError
- Pause and resume thread in java is not working
- Java / Wildfly: Threads piling up / Eclipse shows one additional running thread for each request
- App Engine UserService calls in servlet throw thread exception - Eclipse java
- Java thread program exits before for loop (server side of program) (Eclipse using Mac Terminal/command line arguments)
- Java Thread Start-Stop-Start on same button click in Eclipse
- Exception in thread "main" - Cannot get class with test cases to run as java application or as debugger
- java eclipse can't step through thread after breakpoint with multithreaded program
- Java | Terminating all child threads if parent thread is terminated
More Query from same tag
- logcat text colour: what do the different colours represent?
- SWT Browser in eclipse plugin behaves differently in Mac and Windows
- Organizing non-Maven source code in Eclipse projects
- Eclipse move participant with many files
- Gdb debug "terminated exit value: 0" error eclipse on mac
- How to assign run configurations to icons at the top of Eclipse
- Even though Hibernate is added it is not being imported
- Google calendar API android "GoogleAuthorizationRequestUrl cannot be resolved to a type"
- How to find forward reference in eclipse?
- Find eclipse setting name to set it in plugin_customization.ini
- System workbench for stm32: fatal error ... no such file or directory
- Android sql database
- STS compiles .groovy as .java
- What UML relation should I use between these two classes?
- How to attach Glassfish source in eclipse
- What are the drawbacks of deploying a website using the Eclipse IDE?
- Eclipse Helios-"cannot run program 'make' : unknow reason" on Mac OS X
- Failed to locate library: liblwjgl.dylib
- How to specify dependency on tomcat libraries with gradle
- How to Change a .PNG file name through Java/Eclipse
- Is there a way to convert wildcard imports in java source code to named imports based on the code?
- Change .jar name in MacOS dock in Eclipse?
- How to configure Apache drill within eclipse data source explorer
- eclipse does not treat a scalatest flatspec as junit test
- Android Threaded Canvas problems with Sensors
- Eclipse Auto-Import Android Not Working
- How to force Java compiler to error if @Override is unnecessary
- Eclipse getter/setter format
- Android SDK Setup under Windows 7 Pro 64 bit
- Eclipse and Pydev plugin