【问题标题】:How to call a non-static method from a static method [duplicate]如何从静态方法调用非静态方法[重复]
【发布时间】:2023-03-26 16:05:01
【问题描述】:

可能重复:
calling non-static method in static method in Java

是否可以从静态方法调用非静态方法?非静态位于另一个类中,它必须是非静态的。

    public static void start() {
        CheckConnection checkInternet = new CheckConnection();
        if (checkInternet.isNetworkAvailable()) {
            // Has internet Connection
            } else {
            // No Internet Connection
        }
 }

代码在 Eclipse (Android 4.0.4) 中没有给出任何错误,但如果我运行它,我的应用程序会冻结并关闭。

【问题讨论】:

  • 是的,这是可能的(如果没有,它将无法编译)。这不是它冻结的原因。 isNetworkAvailable() 方法更有可能因为其他原因而冻结。
  • 或 CheckConnection 的负责人。在调试模式下运行时查看堆栈跟踪可以揭示问题。
  • 在 java 程序(不是专门的 android)中 Main 是一个静态方法.. :) .. 它调用一切。所以你的标题的答案是肯定的。就像所有人都在暗示这里发生了其他事情一样。 @AndroSelva 这不是重复的,问题与标题无关

标签: java android static android-4.0-ice-cream-sandwich non-static


【解决方案1】:
The only way to call a non-static method from a static method is you should have
an instance of the class containing the non-static method.

就像你的问题:

 CheckConnection checkInternet = new CheckConnection();
    if (checkInternet.isNetworkAvailable()) {
        // Has internet Connection
        } else {
        // No Internet Connection

你有 CheckConnection 的实例,所以你可以调用它。

因此,静态方法中的非静态方法在您的代码中没有问题,可能是其他原因导致应用程序冻结。

【讨论】:

  • 非常感谢! CheckConnection 确实包含一些错误,现在我明白了!
  • 欢迎您。做一些调试。
【解决方案2】:

是否可以从静态方法调用非静态方法?

是的。

由于您创建了CheckConnection 的对象,您可以使用它的引用调用该方法。否则会是编译时错误。

但如果我运行它,我的应用程序会冻结并关闭。

从静态上下文调用非静态方法不是原因。可能原因在isNetworkAvailable 之内。

【讨论】:

    【解决方案3】:

    调试到您的isNetworkAvailable() 方法。如果可能的话,在该方法内有一个try catch 块并打印异常的堆栈跟踪。

    【讨论】:

      【解决方案4】:

      是的,这是可能的。问题一定出在你的isNetworkAvailable 方法上。

      如果无法从静态方法调用非静态方法,Java 应用程序将无法工作,因为 main 本身就是静态的。

      【讨论】:

        【解决方案5】:

        只需创建包含您要使用的非静态方法的类的对象,然后就可以调用了。

        例如:

        public class Test {
          public static void main(String[] args) {
                System.out.println("This is a static method");
                TestClass class1 = new TestClass();
                class1.nonStatic();
             }
         }
        
        class TestClass{
            public void nonStatic(){
                System.out.println("This is a non-static method");
            }
        }
        

        在你的问题中

        public class A{
           public static void start() {
                CheckConnection checkInternet = new CheckConnection();
                if (checkInternet.isNetworkAvailable()) {
                    // Has internet Connection
                    } else {
                    // No Internet Connection
                }
           }
        }
        
        
        
        public class CheckConnection {
            public boolean isNetworkAvailable()
              {
                //some code
              }
           }
        

        如果是这种情况,那么它应该可以工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多