Tag Archives: static-variable

Syntax error, insert “VariableDeclarators” to complete LocalVariableDeclaration

When you use a variable named static today:

package com.day7.demo4;

public class Pengue {
    String name;
    String sex;
    int love=0;
    int health=60;

    static final String SEX_MALE = "Q-boy";
    static  final String SEX_FEMALE = "Q-girl";

    public Pengue() {
        super();
    }

    public Pengue(String name, String sex, int love, int health) {
        super();
        this.name = name;
        this.sex = sex;
        this.love = love;
        this.health = health;
    }

    public void print() {
        System.out.println("name:"+name+",sex:"+sex+",intimacy:"+love+",Health:"+health);
    }

}

For Penguetest.java, go ahead:

System.out.print("Please select the sex of your Penguin:(1.boy 2.girl)");
            int num = sc.nextInt();
            if (num == 1) {
                 Pengue.SEX_MALE;
            }else if (num ==2) {
                 Pengue.SEX_FEMALE;
            }

The error is as follows:

Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

It is correct after modification as follows:

System.out.print("Please select the sex of your Penguin:(1.boy 2.girl)");
            int num = sc.nextInt();
            if (num == 1) {
                pengue.sex = Pengue.SEX_MALE;
            }else if (num ==2) {
                pengue.sex = Pengue.SEX_FEMALE;
            }

Assign it to a variable, because that’s the same thing as 1; Because SEX_MALE is now constant, avoid this error in the future;