I’m working on a really simple text RPG kind of game for a class. I feel like I get everything fine, but when I run the following classes, I get a compiler error.
This is my “Room” class:
import java.io.*;
import java.util.*;
public class Room {
public static int size;
public static void Room(int n) {
size = n;
}
public static void showSize() {
System.out.println(size);
}
}
This is the class that calls it:
import java.io.*;
import java.util.*;
public class Dungeon {
public static void main(String [] args) {
int mySize = 10;
Room a = new Room(mySize);
a.showSize();
}
}
The weird part is, if I run it without any parameters in the Room() constructor, it’s fine, but when I try to pass in a size (either in a variable or explicitly with an int), I get this:
Dungeon.java:8: cannot find symbol
symbol : constructor Room(int)
location: class Room
Room a = new Room(mySize);
^
1 error
is not a constructor, it is static method. so, when you try
Java looks for constructor with parameter and showing compile time error.
Change it to:
Read more about constructors here.