'may I know why i am getting " not a statement " in this code?

class test {
    static void main(String[] args)
    {
        int arr[] = { 23, 2, 73 };
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " ");

        System.out.println();

        int arr2[] = new int[3];
        arr2[] = { 23, 2, 73  };

        for (int i = 0; i < arr2.length; i++)
            System.out.print(arr2[i] + " ");

    }
}

just need to know the solution to remove the error!! and also why it arise!!?



Solution 1:[1]

  1. Java main functions must be public.

  2. You are using C-style curly braces {} after array declaration.

Java supports the C-style curly braces {} construct for creating an array and initializing its elements when it is declared (source)

Replace

int arr2[] = new int[3]; arr2[] = { 23, 2, 73  };

with

int arr2[] = { 23, 2, 73 };

or

int arr2[] = new int[3];
arr2[0] = 23;
arr2[1] = 2;
arr2[2] = 73;

and the code will compile.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamin R