【问题标题】:How do I call a non static method from a main method? [duplicate]如何从主方法调用非静态方法? [复制]
【发布时间】:2013-06-08 21:25:44
【问题描述】:

例如,我正在尝试做这样的事情

public class Test {

    public static void main(String args[]) {

        int[] arr = new int[5];

        arrPrint(arr);
    }

    public void arrPrint(int[] arr) {

        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);

    }
}

我收到一条错误消息,告诉我无法从静态环境中引用非静态变量。那么,如果这是真的,我将如何在 main 中使用非静态方法?

【问题讨论】:

  • 查看我的回答并询问是否有任何问题

标签: java static non-static


【解决方案1】:

你不能。非静态方法是必须在 Test 类的实例上调用的方法;在你的 main 方法中创建一个 Test 实例:

public class Test {

    public static void main(String args[]) {
        int[] arr = new int[5];
        arr = new int[] { 1, 2, 3, 4, 5 };

        Test test = new Test();
        test.arrPrint(arr);

    }

    public void arrPrint(int[] arr) {
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);

    }
}

【讨论】:

  • 这行得通,但我不知道为什么,所以使用 new 创建一个实例变量以从非静态环境中引用?
  • 方法 arrPrint 被定义为 public void arrPrint(int[]) 所以它是“非静态的”意味着它不能被调用,除非它在 ​​Test 的实例上被调用。如果您声明了您的方法: public static void arrPrint(int[]) 那么您可以调用: Test.arrPrint(arr);在 main 中没有为其创建实例。
【解决方案2】:

您只能使用类实例调用非静态方法,因此您必须使用 new 关键字来创建它。

public class Something {

    public static void main(String args[]) {
        Something something = new Something();
        something.method1();

        new Something().method2();
    }

    public void method1() {
    }

    public void method2() {
    }
}

【讨论】:

  • 不回答问题
  • 哦,相信我,确实如此
  • 我确实在我的示例中使用了 new ,因为我已经编辑了。
【解决方案3】:

new Something().method1() 或 new Something().method2()

【讨论】:

    【解决方案4】:

    简而言之,你不能。因为 main 是一种特殊情况(即只有一个入口点),所以除了静态方法、main 中的变量之外,您不能拥有任何东西。

    【讨论】:

    • 那么我该如何测试我的代码呢?我想尝试打印一些代码,我要摆脱 main 吗?当我这样做时,它告诉我我需要一个主要的。
    【解决方案5】:

    根据您的新示例,解决方案将是:

    public class Test {
    
        public static void main(String args[]) {
            int[] arr = new int[5];
            new Test().arrPrint(arr);
        }
    
        public void arrPrint(int[] arr) {
            for (int i = 0; i < arr.length; i++)
                System.out.println(arr[i]);
    
        }
    }
    

    或者你可以移动

    int[] arr = new int[5];
    

    到静态部分像

    public class Test {
    
        static int[] arr; 
    
        public static void main(String args[]) {
            arr = new int[5]; 
            new Test().arrPrint(arr);
        }
    
        public void arrPrint(int[] arr) {
            for (int i = 0; i < arr.length; i++)
                System.out.println(arr[i]);
        }
    }
    

    但是从良好的编程实践来看,第二个闻起来真的很糟糕

    【讨论】:

    • 您不必发布答案。您可以[edit] 旧的并通知 OP 更新。
    【解决方案6】:

    需要在类的实例上调用非静态方法。要创建实例,请使用 new 关键字,如

    Test instance = new Test();
    

    现在您将能够在实例上调用方法,例如

    instance.arrPrint(arr);
    

    【讨论】:

    • 不回答问题
    • @PaulSullivan OP 询问了how would I ever utilize a non static method inside of a main?。我的例子怎么没有回答这个问题?
    • 阅读整个问题
    • So if that is true how would I ever utilize a non static method inside of a main?
    • @PaulSullivan 这里没有回答哪一部分?我并不是说你错了,但我根本看不出我的答案中缺少什么。
    【解决方案7】:

    non-static -> object 的属性

    static method -> 类本身的属性。

    因此,当方法/变量声明中没有 static 关键字时,如果没有来自静态上下文的类的任何实例,您就不能调用/引用该方法/变量。

    正如其他人建议的那样,在 main 方法中创建主类的新实例(new Test())并调用non-staticarrPrintmethod。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-14
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多