Static member 'java.lang.Thread.sleep(long)' accessed via instance reference 。这是什么原因
时间: 2023-06-16 11:05:03 浏览: 294
这个错误是因为你在使用静态方法时使用了实例化对象来调用它。应该使用类名来调用静态方法,而不是对象实例。
例如,对于Thread类中的静态方法sleep(),应该使用以下方式来调用:
```java
Thread.sleep(1000); // 使用类名调用静态方法
```
而不是:
```java
Thread thread = new Thread();
thread.sleep(1000); // 错误的调用方式,使用实例化对象调用静态方法
```
因此,如果你想在代码中使用Thread.sleep()来暂停执行,就需要使用类名Thread来调用静态方法,而不是创建一个Thread对象来调用它。
相关问题
Static member 'java.lang.String.join(java.lang.CharSequence, java.lang.CharSequence...)' accessed via instance reference
The error message "Static member 'java.lang.String.join(java.lang.CharSequence, java.lang.CharSequence...)' accessed via instance reference" occurs when you try to call the static method `join` on an instance of the `String` class instead of calling it on the class itself.
The `join` method is used to join a group of strings together with a delimiter. It takes two parameters: a delimiter and an array of strings. Here is an example of how to use it:
```
String[] words = {"hello", "world", "how", "are", "you"};
String joinedWords = String.join(" ", words);
System.out.println(joinedWords);
```
In this example, we create an array of strings called `words`, and then use the `join` method of the `String` class to join the strings together with a space delimiter.
If you see the error message "Static member 'java.lang.String.join(java.lang.CharSequence, java.lang.CharSequence...)' accessed via instance reference", it means that you are calling the `join` method on an instance of the `String` class, rather than on the class itself. To fix the error, make sure you call the method on the class, like this:
```
String[] words = {"hello", "world", "how", "are", "you"};
String joinedWords = String.join(" ", words);
System.out.println(joinedWords);
```
Note that in the corrected code, we are calling the `join` method on the `String` class itself, not on an instance of the `String` class.
Static member 'com.measdatamgmt. ExportHdxlDataService. exportHdxlData' accessed via instance reference
在Java中,"Static member 'com.measdatamgmt.ExportHdxlDataService.exportHdxlData' accessed via instance reference" 这种情况通常是指在一个实例上调用了静态方法(Static method),而静态方法通常是属于类而不是对象的。静态方法可以直接通过类名而非对象实例来访问。
`ExportHdxlDataService.exportHdxlData`看起来像是一个名为 `exportHdxlData` 的静态方法,它位于 `ExportHdxlDataService` 类中。如果在一个具体的 `ExportHdxlDataService` 实例上调用了这个方法,尽管它是静态的,但仍然通过实例引用来间接调用。
例如:
```java
// 创建了一个 ExportHdxlDataService 的实例
ExportHdxlDataService service = new ExportHdxlDataService();
// 虽然exportHdxlData是一个静态方法,但我们通过服务对象来调用它
service.exportHdxlData(); // 尽管static,但通过实例引用调用
```
然而,直接使用类名 `ExportHdxlDataService.exportHdxlData()` 是完全可行的,不通过实例也能访问:
```java
ExportHdxlDataService.exportHdxlData(); // 直接通过类名调用静态方法
```
阅读全文