score:144
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.
score:0
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.
score:3
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.
score:33
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:
someOtherThread.sleep(x);
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.
Source: stackoverflow.com
Related Query
- 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
- Haskell Eclipse Linux and Buildwrapper
- Ant: Replace tokens in source file multiple times
- eclim not doing autocompletion in vim
- Eclipse plugin - wrong jar used on runtime
- Importing Eclipse project to Android Studio 0.8.14
- Singling out web.xml errors in Eclipse
- How to generate pom file for existing eclipse plugin project?
- Eclipse OverviewRuler not showing annotations
- Beyond Studio For NXP Unresolved Inclusions
- Applet with dependencies in Eclipse
- Exporting runnable jar with eclipse
- Java pseudo timer
- Changing the package name of my Android project
- Eclipse Juno - unable to attach Android API documentation archive file
- Eclipse: how to remove a directory tree from a CVS repository?
- SQLAlchemy returns an integer
- Integrate a plug-in to Eclipse IDE
- Cocos2dx - android apk crashes on start
- Inheritance Errors in Java
- how to insert new line in eclipse using Preference and formatter?
- Google chrome with seleneium web driver
- How to add coldfusion server on CFEclipse
- Eclipse Project Collaboration Remotely
- Java - Make src/main/java compile with java 1.4 and src/test/java compile with 1.5
- Trim trailing space in jasypt EncryptablePropertyPlaceholderConfigurer
- Is there a way to find/replace across an entire project in Eclipse?
- Eclipse Android Graphical Layout Editor Broken
- Debugging Java projects where a .jar file contains the main method
- What is a goal when running a Maven/Wicket project in Eclipse?
- How to set up Eclipse + StatET + Rcpp on Windows