Smit Patel
Smit Patel

Reputation: 7

Declare a private String array and initialization of array elements giving an error

I declare an array like this

    private String[] phoneNumber = new String[3];

    phoneNumber[0] = {"hi"};
    phoneNumber[1] = {"hello"};
    phoneNumber[2] = {"hola"};

IDE says "Unknown class: 'phoneNumber'"

There is something dumb I am doing but I did not get it so I come for a help.

Some questions raise to my is below:
1) Array cannot be declare as String?
2) If the answer to the first question is yes, then how?
3) What I am doing wrong?

Upvotes: 0

Views: 652

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201537

You need an initialization block to do that, like

private String[] phoneNumber = new String[3];
{
    phoneNumber[0] = "hi";
    phoneNumber[1] = "hello";
    phoneNumber[2] = "hola";
}

or use the shorter array literal syntax. Like,

private String[] phoneNumber = { "hi", "hello", "hola" };

Upvotes: 1

Related Questions