Cannot make a static reference to the non-static method show() from the type Student
时间: 2023-11-03 17:10:46 浏览: 151
这个错误是因为你尝试在静态方法中访问非静态方法。静态方法是属于类的,而非静态方法是属于实例的。因此,当你在静态方法中使用非静态方法时,你必须先实例化对象,才能够访问这个非静态方法。
如果你想在静态方法中访问非静态方法,你需要创建一个对象,然后使用这个对象来调用非静态方法。例如:
```
public class Student {
public void show() {
// ...
}
public static void main(String[] args) {
Student student = new Student();
student.show(); // 调用非静态方法
}
}
```
这里我们先创建了一个 `Student` 对象,然后使用这个对象来调用 `show()` 方法。这样就可以避免这个错误了。
相关问题
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();
```
这样就可以正确地调用该方法了。
阅读全文