1
public class Monster{

public final String TOMBSTONE = "Here Lies a Dead monster";


private int health = 500;
private int attack = 20;
private int movement = 2;

public String name = "Big Monster";


public int getAttack()
{
    return attack;
}

public int getMovement()
{
    return movement;
}

public int getHealth()
{
    return health;
}

public Monster(int health, int attack, int movement)
{
    this.health = health;
    this.attack = attack;
    this.movement = movement;

}

public Monster()
{

}}


public class Frank {

public static void main(String[] args){

    Monster NewMonster = new Monster();

    NewMonster.name = "Frank";

    System.out.println(NewMonster.name + " has an attack value of " + NewMonster.getAttack());

}

}

When trying to create a new object from my Monster class I get this error:

Frank.java:5: error: cannot find symbol
            Monster NewMonster = new Monster();
            ^

symbol: class Monster location: class Frank

I am very new to Java so sorry if this is a simple/easy fix but everything I have researched does not give me a solution to this error.

Thanks in advance for any replies/feedback.

GS2VT
  • 29
  • 3
  • Does this answer your question? [What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-or-cannot-resolve-symbol-error-mean) – OH GOD SPIDERS May 27 '20 at 09:25
  • Are your classes in separate files, `Monster.java` and `Frank.java` ? Are they in the same package ? – Marc Le Bihan May 27 '20 at 09:32

2 Answers2

0

You are not allowed to have multiple public classes in one file in Java. Therefore you need to either remove a public modifier of one of your classes or put the main method in your public class. I did the latter, since it is not necessary to place the main method in a seperate class just to instantiate your object.

I have updated your code and it runs now

public class Monster{

    public static void main(String[] args) {

        Monster NewMonster = new Monster();

        NewMonster.name = "Frank";

        System.out.println(NewMonster.name + " has an attack value of 
        " + NewMonster.getAttack());
    }

    public final String TOMBSTONE = "Here Lies a Dead monster";


    private int health = 500;
    private int attack = 20;
    private int movement = 2;

    public String name = "Big Monster";


    public int getAttack() {
        return attack;
    }

    public int getMovement(){
        return movement;
    }

    public int getHealth(){
        return health;
    }

    public Monster(int health, int attack, int movement){
        this.health = health;
        this.attack = attack;
        this.movement = movement;
    }

    public Monster(){}
}
Daniel Jacob
  • 1,455
  • 9
  • 17
  • Thank you! If I was to put the classes into two seperate files, how I would I import one into the other? – GS2VT May 27 '20 at 10:03
  • You don't need to if those classes are within the same package. For the scope of this example they would be in the same package and you don't need to import the class. – Daniel Jacob May 27 '20 at 10:37
0

1 You have to define only one class a public which will be saved as per your java file name (.java)

2 object references will be always in lower case

class Monster {

public final String TOMBSTONE = "Here Lies a Dead monster";

private int health = 500;
private int attack = 20;
private int movement = 2;

public String name = "Big Monster";

public int getAttack() {
    return attack;
}

public int getMovement() {
    return movement;
}

public int getHealth() {
    return health;
}

public Monster(int health, int attack, int movement) {
    this.health = health;
    this.attack = attack;
    this.movement = movement;

}

public Monster() {

}

} // only Frank can be a public class in a single java file, or else create two different java classes and import. public class Frank {

public static void main(String[] args){

Monster newMonster = new Monster();

newMonster.name = "Frank";

System.out.println(newMonster.name + " has an attack value of " + newMonster.getAttack());

} }

Output: Frank has an attack value of 20