Results 1 to 5 of 5

Thread: Simple newbish error, public classes should be in its own file?

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Question Simple newbish error, public classes should be in its own file?

    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?
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    A Java source file can contain more than one class definition. Sun's JDK enforces the rule that at most one class in the source file must have public accessibility. The name of this source file is the name of the public class with the .java extension. You will notice that when you compile a source file that contains multiple classes separate .class files will be created.

  3. #3
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    save the code to CountInstances.java

  4. #4

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    i'm still alittle confused on why it brought up an error, and the file was already saved as a .java file. You said, at most one class in the source file must have public accessibility. So because the main function is , public static void main(.... it will give me a compiler error because its now seeing 2 different public function because I had the CountInstances as a public class also? Thanks for the responces.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You seem to be confusing yourself. Your code runs fine as long as you name the .java class file after the public class you posted before.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width