• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding public classes

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks ,
As I was going through the concept of classes in java..there I read that there should be one public class in source file ...!! My query is that it is mandatory to have only public class in the sourcefile ..what about if I kept several public classes in the sorce file and inside one public class I kept the main() method...will that case work ..!!
 
Cole Terry
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's not mandatory. The rule is one source file should have 0 or 1 public class, no more. Otherwise the compiler will complain.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be only one public class and any number of non public classes, and public class should match with the file name.
you can have all non public classes in a file.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:what about if I kept several public classes in the sorce file and inside one public class I kept the main() method...will that case work ..!!

What happened when you tried it out? (Experimenting is the best way to learn new things!).
 
Karthikeyan Nadanasabapathy
Greenhorn
Posts: 11
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The public type "Some class/interface name" must be defined in its own file error will be thrown by the compiler
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic