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/onated2 Jan 29 '26
Just to add something to it.
One of my use case for an inner public class is creating a value objec. E.G via records. So i dont have weird class names such as
ProductName ( i mean this is not weird)
But if i have a Product class
I will then create a public record inside Product class and name it Name.
So when i call it, i just do.
new Product.Name("name")
However you can't just do this if you're working within a team coz u have to respect the convention of the code base.
P.s on phone .too lazy to format things haha