Cannot make a static reference to the non-static method method() from the type test
时间: 2023-11-03 12:53:42 浏览: 191
这个问题是因为你尝试在静态方法中引用非静态方法。在 Java 中,静态方法属于类而不是对象,它们在对象创建之前就可以调用。而非静态方法则需要通过对象来调用。
如果你想在静态方法中引用非静态方法,你需要先创建一个对象,然后通过该对象来调用非静态方法。例如:
```java
public class Test {
public static void main(String[] args) {
Test obj = new Test();
obj.method(); // 调用非静态方法
}
public void method() {
// 非静态方法的实现
}
}
```
这样做就可以访问非静态方法了。希望能帮助到你!如果还有其他问题,请随时提出。
相关问题
Cannot make a static reference to the non-static method login() from the type yGym
This error message indicates that you are trying to call a non-static method from a static context. In Java, non-static methods belong to an instance of a class, whereas static methods belong to the class itself.
To resolve this error, you need to either make the method static or create an instance of the class and call the method on that instance.
If you want to make the method static, you can add the "static" keyword to the method signature. For example:
```java
public static void login() {
// method implementation
}
```
Alternatively, if you want to call a non-static method, you need to first create an instance of the class and then call the method on that instance. For example:
```java
yGym gym = new yGym();
gym.login();
```
I hope this helps to resolve your error!
Cannot make a static reference to the non-static method getIndexNewsList() from the type IndexNewsDao
这个错误是因为你在调用一个非静态方法时使用了类名来进行调用,但是该方法是非静态方法,只能通过对象来进行调用。你需要先创建一个IndexNewsDao对象,然后通过该对象来调用getIndexNewsList()方法,例如:
```
IndexNewsDao dao = new IndexNewsDao();
List<News> newsList = dao.getIndexNewsList();
```
这样就可以正确地调用该方法了。
阅读全文