-1

I have a homework assignment where I have to pass a 2D array to a method, that method takes a 2D array as a parameter and prints out the table. I have the table working fine, the problem is I cannot figure out how to call this method from my main method without a constructor.

I know the obvious solution is to simply make a constructor method first and use that but, I unfortunately cannot because of the requirements of the homework.

Can anyone please just tell me how I can call this method, pass the param and have it print out the table, from the main method WITHOUT making a constructor method? Thank you.

As is I am getting: pgm1.java:75: error: non-static method arrays(int[][]) cannot be referenced from a static context
arrays(tenBy); ^ 1 error

public class pgm1

{

public void arrays(int[][] userArray)
{

    int rowTotal = 0;
    int colTotal = 0;
    int allTotal = 0;       


    //For loop to populate array, find total values of all odd rows,
    //all even columns, and all total index values
    for (int i = 0 ; i < userArray.length ; i++)
    {

        for (int h = 0 ; h < userArray.length ; h++)
        {           
            userArray[i][h] = i * h;
            System.out.printf("%3d" , userArray[i][h]);

            //Running total of all index values
            allTotal += userArray[i][h];

            //Running total of all odd rows
            if (i % 2 == 1)
                rowTotal += userArray[i][h];

            //Running total of all even columns
            if (h % 2 == 0)
                colTotal += userArray[i][h];                    
        }
        System.out.println();           

    }

    //Print all totals
    System.out.println("\n Total of odd numbered rows: " + rowTotal);
    System.out.println(" Total of even numbered columns: " + colTotal);
    System.out.println(" Total of all numbers: " + allTotal);
}


public static void main(String[] args)
{

    //Creating 2D Array
    int[][] tenBy = new int[10][10];

    //arrays();

    arrays(tenBy);      

}

}

hazy7687
  • 67
  • 8

1 Answers1

1

Change

public void arrays(int[][] userArray)

to

public static void arrays(int[][] userArray)

JLS-8.4.3.2. static Methods says (in part),

A method that is declared static is called a class method.

and

A method that is not declared static is called an instance method, and sometimes called a non-static method.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249