-1
public class Weapons
{
    private String[] weaponType;
    
    public Weapons()
    {   
        this.weaponType[] = {"Melee", "Range", "Magic"};
    }   

}

It's giving me 2 errors.

Syntax error, insert ";" to complete BlockStatements

Syntax error, insert "AssignmentOperator Expression" to complete Assignment

How do I properly assign the elements inside the array from an instance variable of arrays?

0

1 Answer 1

3
    this.weaponType[] = {"Melee", "Range", "Magic"};

Should be

this.weaponType = new String [] {"Melee", "Range", "Magic"};

You missed the type while assigning it and you need not to add those [] again while assigning.

And if you want to move that assignment to top because of that static data, you can simply write

private String[] weaponType ={ "Melee", "Range", "Magic" };
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.