How do I fix the error where I cannot make a static reference to a non-static input field?

后端 未结 5 666
刺人心
刺人心 2021-01-26 22:38

I am learning java. I wrote the following code but I am getting this error \"cant make a static reference to a non static input field\" in Arrayfunction(), when I try to take in

5条回答
  •  面向向阳花
    2021-01-26 22:57

    input in your class is an instance variable (since it's not defined as static), which means that each instance of MultidimArrays has one of it's own. static fields or methods (often referred to as "class variables/methods" are shared between all instances of a class.

    Since Arrayfunction is static, it cannot refer to instance members of its class - there is no way for it to know which MultidimArray to use. You can either solve this by making input itself static, or by removing the static qualifier from ArrayFunction and create an instance of your class:

    public static void main(String args[])
    {       
        int array[][] arr = new int[2][3]; //typo here, variable needs a name :)
    
        System.out.println("Passing array to a function");
        MultidimArray ma = new MultidimArray();
        ma.Arrayfunction(arr);
    }
    

提交回复
热议问题