如何摆脱这种“静态方法应该在静态的方式中被嵌入”在java中?

时间:2022-04-01 07:08:56

I have the following piece of code in a java application

我在java应用程序中有以下代码段。

Thread.currentThread().sleep(10000);

However eclipse is showing me the following warning:

但是eclipse向我展示了以下警告:

The static method sleep(long) from the type Thread should be accessed in a static way

I am very proud of never release code with warnings, and I would like to get rid of this warning (it occurs in two different classes). Do I need to post the entire code?

我非常自豪永远不会发布带有警告的代码,我想要消除这个警告(它发生在两个不同的类中)。我是否需要发布整个代码?

5 个解决方案

#1


32  

You call

你叫

Thread.sleep(10000);

It always makes the current thread sleep. Even if you did:

它总是使当前线程休眠。即使你做的:

Thread t = new Thread(...);
t.start();
t.sleep(10000);

That would still make the current thread sleep for 10 seconds, while leaving the new thread to go on its merry way. This is almost the canonical example for why this warning is important - it's because you're calling a static method as if it were an instance method, which makes it look like it matters what you're calling it on. It doesn't. The value isn't even checked for nullity:

这仍然会使当前线程休眠10秒,而离开新线程继续它的愉快方式。这几乎就是为什么这个警告很重要的典型示例——因为您调用的是静态方法,就像它是一个实例方法一样,这使得它看起来很重要。它不是。这个值甚至没有被检查为无效:

Thread t = null;
t.sleep(10000); // Still sleeps for 10 seconds...

(I'm proud to say I originally filed a feature request for this warning in Eclipse back in June 2002 :)

(我很自豪地说,我最初在2002年6月向Eclipse提交了这个警告的特性请求:)

#2


3  

Thread.sleep(...) (a static method on Thread).

线程.sleep(…)(线程上的静态方法)。

Causes the currently executing thread to sleep ...

导致当前执行线程休眠…

It doesn't make sense to tell another thread to sleep: making it static ensures this restriction (albeit "only with a warning").

告诉另一个线程睡眠是没有意义的:让它静态确保这个限制(尽管“只有警告”)。

The code in the post will compile because obj.sm is rewritten by the compiler to T.sm, where sm is a static method on class T and the compile-time type of obj is T: as such it is the static method which is invoked and not an instance method (for a particular Thread), which is what the warning is about.

post中的代码将被编译,因为obj。sm被编译器重写为T。sm是类T的静态方法,而obj的编译时类型为T:它是被调用的静态方法,而不是实例方法(对于特定的线程),这是警告的内容。

Happy coding.

快乐的编码。

#3


2  

The way to go is

去的路是。

Thread.sleep(10000);

With the intention, that you can send to sleep only yourself anyways. A real non-static method would imply, that you could send another thread sleeping too.

有了这个意图,你就只能让自己睡觉了。一个真正的非静态方法意味着,您可以发送另一个线程睡眠。

#4


2  

Just call Thread.sleep(10000), it will cause the current thread to sleep. http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#sleep(long)

只需调用thread .sleep(10000),它将导致当前线程休眠。http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html睡眠(长)

#5


2  

Yup, when you look at the doc for sleep() it says "static void sleep(long millis)". The "static" isn't there because of noise in the channel, it means that the method should be addressed Thread.sleep(...) instead of someThreadObject.sleep(...). As a "convenience" you can use the latter form, but it's strongly discouraged.

是的,当你看《睡眠医生》(doc for sleep)时,它说的是“静空睡眠(长米)”。“静态”不是因为通道中的噪声而存在,而是意味着该方法应该被处理为Thread.sleep(…)而不是someThreadObject.sleep(…)。作为一种“便利”,你可以使用后一种形式,但它强烈地不鼓励。

#1


32  

You call

你叫

Thread.sleep(10000);

It always makes the current thread sleep. Even if you did:

它总是使当前线程休眠。即使你做的:

Thread t = new Thread(...);
t.start();
t.sleep(10000);

That would still make the current thread sleep for 10 seconds, while leaving the new thread to go on its merry way. This is almost the canonical example for why this warning is important - it's because you're calling a static method as if it were an instance method, which makes it look like it matters what you're calling it on. It doesn't. The value isn't even checked for nullity:

这仍然会使当前线程休眠10秒,而离开新线程继续它的愉快方式。这几乎就是为什么这个警告很重要的典型示例——因为您调用的是静态方法,就像它是一个实例方法一样,这使得它看起来很重要。它不是。这个值甚至没有被检查为无效:

Thread t = null;
t.sleep(10000); // Still sleeps for 10 seconds...

(I'm proud to say I originally filed a feature request for this warning in Eclipse back in June 2002 :)

(我很自豪地说,我最初在2002年6月向Eclipse提交了这个警告的特性请求:)

#2


3  

Thread.sleep(...) (a static method on Thread).

线程.sleep(…)(线程上的静态方法)。

Causes the currently executing thread to sleep ...

导致当前执行线程休眠…

It doesn't make sense to tell another thread to sleep: making it static ensures this restriction (albeit "only with a warning").

告诉另一个线程睡眠是没有意义的:让它静态确保这个限制(尽管“只有警告”)。

The code in the post will compile because obj.sm is rewritten by the compiler to T.sm, where sm is a static method on class T and the compile-time type of obj is T: as such it is the static method which is invoked and not an instance method (for a particular Thread), which is what the warning is about.

post中的代码将被编译,因为obj。sm被编译器重写为T。sm是类T的静态方法,而obj的编译时类型为T:它是被调用的静态方法,而不是实例方法(对于特定的线程),这是警告的内容。

Happy coding.

快乐的编码。

#3


2  

The way to go is

去的路是。

Thread.sleep(10000);

With the intention, that you can send to sleep only yourself anyways. A real non-static method would imply, that you could send another thread sleeping too.

有了这个意图,你就只能让自己睡觉了。一个真正的非静态方法意味着,您可以发送另一个线程睡眠。

#4


2  

Just call Thread.sleep(10000), it will cause the current thread to sleep. http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html#sleep(long)

只需调用thread .sleep(10000),它将导致当前线程休眠。http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html睡眠(长)

#5


2  

Yup, when you look at the doc for sleep() it says "static void sleep(long millis)". The "static" isn't there because of noise in the channel, it means that the method should be addressed Thread.sleep(...) instead of someThreadObject.sleep(...). As a "convenience" you can use the latter form, but it's strongly discouraged.

是的,当你看《睡眠医生》(doc for sleep)时,它说的是“静空睡眠(长米)”。“静态”不是因为通道中的噪声而存在,而是意味着该方法应该被处理为Thread.sleep(…)而不是someThreadObject.sleep(…)。作为一种“便利”,你可以使用后一种形式,但它强烈地不鼓励。