在使用 Java 代码进行开发时,可能会遇到以下错误:

 if (captcha != null && captcha.equalsIgnoreCase(sessionCaptcha)) {
     response.getWriter().write('验证码正确');
     
     com.Test.demo1.tjChaXun();
     
 } else {
     response.getWriter().write('验证码错误');
 }

问题

com.Test.demo1.tjChaXun() 报错原因:Cannot make a static reference to the non-static method tjChaXun() from the type demo1

原因

这个错误是因为在调用方法时,使用了类名来调用非静态方法,而该方法是非静态方法,需要通过对象来调用。

解决方法

  1. 创建对象调用方法: 创建一个 demo1 类的对象,然后通过该对象来调用 tjChaXun() 方法。

    demo1 obj = new demo1();
    obj.tjChaXun();
    
  2. 将方法改为静态方法:tjChaXun() 方法改为静态方法,这样就可以直接通过类名来调用。

    public static void tjChaXun() {
        // 方法实现
    }
    

通过以上两种方法,就可以解决 Cannot make a static reference to the non-static method tjChaXun() from the type demo1 错误。

Java 异常解决:Cannot make a static reference to the non-static method tjChaXun()

原文地址: https://www.cveoy.top/t/topic/f1Ex 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录