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 input. Why is this so and How can I fix it?
import java.util.*;
public class MultidimArrays {
Scanner input= new Scanner(System.in);
public static void main(String args[])
{
int array[][]= new int[2][3];
System.out.println("Passing array to a function");
Arrayfunction(array);
}
public static void Arrayfunction(int array[][])
{
System.out.println("Inside Array function");
for(int i=0;i<array.length;i++)
{
for(int j=0;j<array[i].length;j++)
{
System.out.println("Enter a number");
array[i][j]=input.nextInt();// error
}
}
}
Scanner is not defined as static therefore is in the wrong scope
Either create the Scanner instance inside
Arrayfunctionor create your scanner with