Java: Thread.currentThread().sleep(x) vs. Thread.sleep(x)

后端 未结 4 739
清歌不尽
清歌不尽 2020-12-04 13:33

I have this in my code

Thread.currentThread().sleep(x);

Eclipse tells me to use the static

Thread.sleep(x); 
4条回答
  •  独厮守ぢ
    2020-12-04 13:53

    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.

提交回复
热议问题