Java: how to declare an array and quickly fill it with data?

public static void main(String[] args) throws IOException {
    String st3[]=new String[]{"", "", "", "sdf", "sdfsd", "sdfsd"};
    System.out.println(st3[1]);
}

The second line of Netbeans displays an error message:

"non-static variable cannot be referenced from a static context".

I know the problem is declaring an array. How to declare an array STRING and quickly fill it with data?

Sorry for the stupid question and very poor english.

Thank you very much for the answers, the error has been resolved. :)

+3
source share
2 answers

The problem is not to declare an array at all. You have not shown enough code to show what is really wrong with it, but these are not the lines. The initialization of the array is a bit long, but it is valid.

Please show a short but complete program that demonstrates the problem.

What method are these lines in?

, , :

public class Test {
    public static void main(String[] args) {
        String st3[]=new String[]{"x", "y", "z", "sdf", "sdfsd", "sdfsd"};
        System.out.println(st3[1]);
    }
}
+13

:

String[] st3 = {"", "", "", "sdf", "sdfsd", "sdfsd"};
+1

Source: https://habr.com/ru/post/1772094/


All Articles