简体   繁体   中英

How to fix “Error: Cannot find symbol” when creating an object?

I don't understand how my compiler cannot find the class Weight , considering didn't I already make it in my public boolean checkWeight method? The compiler points to the line:

`Weight first = new Weight();`

and

`Weight second = new Weight();`

public class Guard {

    int stashWeight;

    public Guard ( int maxWeight ) {
        this.stashWeight = maxWeight;
    }

    public boolean checkWeight (Weight maxWeight) {
        if ( maxWeight.weight >= stashWeight ) {
            return true;
        } else {
            return false;
        }
    }

    public static void main (String[] args ) {
        Weight first = new Weight();
        first.weight = 3000;
        Weight second = new Weight();
        second.weight = 120;

        Guard grams = new Guard(450);
        System.out.println("Officer, can I come in?");
        boolean canFirstManGo = grams.checkWeight(first);
        System.out.println(canFirstManGo);

        System.out.println();

        System.out.println("Officer, how about me?");
        boolean canSecondManGo = grams.checkWeight(second);
        System.out.println(canSecondManGo);

    }
}

you should define the Weight class like this:

public class Weight {

    public int weight;

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}

Output:

Officer, can I come in?
true

Officer, how about me?
false

Or import your Weight class using import keyword.

your Weight class is missing in your code See the below code

class Weight {

    public int weight;

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}

public class Guard {

    int stashWeight;

    public Guard(int maxWeight) {
        this.stashWeight = maxWeight;
    }

    public boolean checkWeight(Weight maxWeight) {
        if (maxWeight.weight >= stashWeight) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        Weight first = new Weight();
        first.weight = 3000;
        Weight second = new Weight();
        second.weight = 120;

        Guard grams = new Guard(450);
        System.out.println("Officer, can I come in?");
        boolean canFirstManGo = grams.checkWeight(first);
        System.out.println(canFirstManGo);

        System.out.println();

        System.out.println("Officer, how about me?");
        boolean canSecondManGo = grams.checkWeight(second);
        System.out.println(canSecondManGo);

    }
}

output

**

Officer, can I come in?
true
Officer, how about me?
false

**

Is your Weight class in a different package? What's the access modifier of the Weight class?

Please verify above and have a look in below document.

Access Modifiers

  1. The method parameter "maxWeight" is of type "Weight" for which Java is unable to find the definition to resolve your program dependencies. Failing to find the class over classpath will lead to the error "Cannot find symbol". You may want to define the class "Weight" and import it, like others have responded to you.
  2. Do you really need the "Weight" class? You are simply comparing the passed argument with the "Guard's" stashWeight.

Your public method could simply be:

public boolean checkWeight(int maxWeight) {
        return maxWeight >= stashWeight;

}

And the main method could be updated as:

public static void main(String[] args) {
    int firstWeight = 3000;
    int secondWeight = 120;

    Guard grams = new Guard(450);
    System.out.println("Officer, can I come in?");
    boolean canFirstManGo = grams.checkWeight(firstWeight);
    System.out.println(canFirstManGo);

    System.out.println();

    System.out.println("Officer, how about me?");
    boolean canSecondManGo = grams.checkWeight(secondWeight);
    System.out.println(canSecondManGo);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM