Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding public classes  RSS feed

 
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 ..!!
 
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.
 
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.
 
Java Cowboy
Sheriff
Posts: 16081
88
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!).
 
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!