• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

regarding public classes

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic