r/javahelp • u/Miserable_Bar_5800 • 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
View all comments
Show parent comment
2
u/JarnisKerman Jan 29 '26
Just because you can do it doesn’t make it a good idea. When working with Java, you should follow the conventions, both generally accepted ones and the ones agreed upon in your team. Having a bunch of package-private classes in the same files as your public classes is not conventional and should be avoided.
Using inner classes is ok in certain cases, you should read up on the best practices around inner classes.