r/javahelp • • Jan 29 '26

Can I have 2 public classes?

Guys can anyone teach me?

We all know:

public class Main {

Whatever we put after public class it needs to be the file name, like in that it needs Main.java

What if we add two public classes like:

public class Main {
}

public class ootherMain {
}

I'm just curious on what happens I think it gets an error or is there a way to actually do that?

0 Upvotes

16 comments sorted by

View all comments

Show parent comment

3

u/MinimumBeginning5144 Jan 29 '26

I agree. Convention is a human aspect, not a technical one, but it's still very important.

Note this is just a Java convention. In other languages you often find multiple classes declared in the same file. Including Kotlin on the JVM, where for example you might find a number of closely-related exception classes in the same file.