19

Ok, this might be kiddies question in java. We can't define two public classes in one file. But, in one of the examples from the book SCJP study guide, this example was mentioned:

public abstract class A{
    public abstract void show(String data);
}

public class B extends A{
    public void show(String data){
        System.out.println("The string data is "+data);
    }
    public static void main(String [] args){
        B b = new B();
        b.show("Some sample string data");
    }
}

When I copy pasted this into netbeans immediately compile error was thrown, that public class A should me mentioned in separate file. Is that example from SCJP styudy guide really wrong? Also in some of the mock test I found many questions having such pattern but in none of the options was a compiler error was mentioned. Getting worried here

8
  • 3
    Did it say the classes should be in the same file? Commented Jun 24, 2012 at 13:06
  • Agree, those two classes must be in separate files. Commented Jun 24, 2012 at 13:06
  • You shouldn't worry about the book; if you know what's in that book, you'll pass with flying colors. Commented Jun 24, 2012 at 13:07
  • @Puce Nothing such mentioned, but when an example like this is mentioned without any break, java novice will put it one file and banggg Commented Jun 24, 2012 at 13:09
  • They're probably just put together in the book to make it compact and easily readable. Commented Jun 24, 2012 at 13:33

6 Answers 6

19

yes, 2 top level public classes are not allowed in one file

Sign up to request clarification or add additional context in comments.

5 Comments

then how come such a reputed study guide of SCJP is making such a blunder??
Probably it doesn't say that those two classes should be in the same file. Mistake by Java novice ... not by study guide :-)
Actually I'm pretty sure that the JLS doesn't demand the "one public top level class per file" but only recommends it.
@Voo JLS doesn't even demand files, for that matter. However, "If and only if packages are stored in a file system, the host system may choose to enforce the restriction that it is a compile-time error ..." JLS 7.6. Notice the "may" -- that confirms your hunch.
The reason for "2 top level public classes are not allowed in one file", is that there can only be one public class per .java file, as public classes must have the same name as the source file.
5

Well, if one is being so picky: you can have multiple classes defined with a public modifier in the same file, that is, using the static nested(inner) class. like this:

File -> Test.java

public class Test {

    public static class SomeNestedClass {

    }

}

Comments

1

Yes you can have two classes in the same file. You can have them by removing the public access modifier from both the class name, like shown below,

abstract class A{
    public abstract void show(String data);
}

class B extends A{
    public void show(String data){
        System.out.println("The string data is "+data);
    }
    public static void main(String [] args){
        B b = new B();
        b.show("Some sample string data");
    }
}

1 Comment

then they are package-private, not public
0

you can make 2 public classes in one file , inside a class that contains them .

it's also recommended to add "static" for them , if you do not need any reference to the container class .

Comments

0

You can put two public classes in one file, for example in the file Circle.java:

public class Test {
    public static void main(String args[]) {
        double cir = Circle.findCircumference(7.5);
        System.out.print("Circumference of circle=" + cir);
    }
}
public class Circle {
    public static double findCircumference(double radius) {
        return 2 * Math.PI * radius;
    }
}

If you then run javac Circle.java, you will get an error:

Circle.java:1: error: class Test is public, should be declared in a file named Test.java
public class Test {
       ^
1 error

But if you run it with java Circle.java, then it will work. Why? Probably because the java command, since java 11 (see here), can run also single source-file programs.

Comments

-2

Imagine you could place two public classes in one file, then think about the work of the compiler: it has to build a .class file from your .java file that represents exactly one class (otherwise the .class ending wouldn't make any sense).

The way the JAVA Compiler works it will simply create a .class file with the name of your file and will search for the class with the name of the file in your given file – so it depends on your file name which class will be correctly compiled and which will not.

Long story short: no, you can't put two public classes in one file because the compiler wouldn't be able to handle that correctly.

(Edit: it of course is possible to define new classes INSIDE the one public class that has the same name as your file.)

3 Comments

JAVAC does NOT generate .class files on the basis of source file's name (.java). E.g.: If abc.java has a class DEF and is compiled, it will generate a DEF.class file.
C# allows multiple public classes in one file just fine. I realize Java's compilation procedure wasn't designed for this, so there are language-specific reasons for not implementing it. Wish they would change it to work like C#'s system, though; it's very annoying.
Even scala -another jvm language - handles multiple top level/public classes in a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.