-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?

Community
  • 1
  • 1
puretppc
  • 3,232
  • 8
  • 38
  • 65

1 Answers1

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" };
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307