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.
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(){}
}
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
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.