3
public static void main(String[] args) throws IOException {
    String st3[]=new String[]{"сто", "двести", "триста", "sdf", "sdfsd", "sdfsd"};
    System.out.println(st3[1]);
}

In second line Netbeans displays an error:

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

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

Sorry for stupid question and very bad english.

Thanks very much for answers, error resolved. :)

2

2 Answers 2

17

The problem isn't to do with declaring an array at all. You haven't shown enough code to show what really is wrong with it, but it's not those lines themselves. The array initialization is a little bit long-winded, but it's valid.

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

Which method are these lines in?

Here's a short but complete program which does work:

public class Test {
    public static void main(String[] args) {
        String st3[]=new String[]{"x", "y", "z", "sdf", "sdfsd", "sdfsd"};
        System.out.println(st3[1]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The shortest way to declare and fill in an array:

String[] st3 = {"сто", "двести", "триста", "sdf", "sdfsd", "sdfsd"};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.