Hello everyone, i'm new to java, and i'm using the java 1.4 sdk SE compiler. I just copied and pasted this books source code to make sure I didn't make any typo's and it still brings up this error when i compile:
Code:C:\TEST>javac count.java count.java:31: class CountInstances is public, should be declared in a file name d CountInstances.java public class CountInstances { ^ 1 error
Here's the code, all its doing is using the constructor to count the number of instances i've created.
Code:public class CountInstances { private static int numInstances = 0; protected static int getNumInstances() { return numInstances; } private static void addInstance() { numInstances++; } CountInstances() { CountInstances.addInstance(); } public static void main(String[] arguments) { System.out.println("Starting with " + CountInstances.getNumInstances() + " instances"); for (int i = 0; i < 10; ++i) new CountInstances(); System.out.println("Created " + CountInstances.getNumInstances() + " instances"); } }
THanks!
By the way when I take out the modifer public the code compiles fine, but why do I have to take the keyword public off the class? And why does this book suck so bad?![]()



Reply With Quote
Your code runs fine as long as you name the .java class file after the public class you posted before.
