aspose file tools*
The moose likes Java in General and the fly likes regarding public classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regarding public classes" Watch "regarding public classes" New topic
Author

regarding public classes

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

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

Joined: Nov 23, 2011
Posts: 45
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

Joined: Jun 26, 2008
Posts: 237

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.


Sudheer
SCWCD, SCJP 5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

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!).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Karthikeyan Nadanasabapathy
Greenhorn

Joined: Dec 13, 2011
Posts: 11

The public type "Some class/interface name" must be defined in its own file error will be thrown by the compiler
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: regarding public classes