How do I call a non static method from a main method? [duplicate]

半城伤御伤魂 提交于 2020-05-08 07:58:50

问题


For example, I am trying to do something like this

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]);

    }
}

I get an error telling me that I can't reference non-static variables from static enviorments. So if that is true how would I ever utilize a non static method inside of a main?


回答1:


You can't. A non-static method is one that must be called on an instance of your Test class; create an instance of Test to play with in your main method:

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]);

    }
}



回答2:


You can call non-static method only using a class instance, so you have to create it using new keyword.

public class Something {

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

        new Something().method2();
    }

    public void method1() {
    }

    public void method2() {
    }
}



回答3:


new Something().method1() or new Something().method2()




回答4:


In short you can't. As main is a special case (i.e. entry point of which there an only be one) you can't have anything other than static methods, variables in main.




回答5:


As per your new example the solution will be:

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]);

    }
}

Or you can move

int[] arr = new int[5];

to the static section like

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]);
    }
}

But the second one smells really bad from point of good programming practices




回答6:


Non static methods need to be invoked on instance of class. To create instance use new keyword like

Test instance = new Test();

now you will be able to invoke methods on instance like

instance.arrPrint(arr);



回答7:


non-static -> property of the object

static method -> property of the class it-self.

So when there is no static keyword in a method/variable declaration you CAN NOT invoke/make reference to that method/variable without any instance of the class from a static context.

As everyone else suggested create a new instance(new Test()) of the main class in main method and invoke non-static arrPrintmethod.



来源:https://stackoverflow.com/questions/17004003/how-do-i-call-a-non-static-method-from-a-main-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!