The moose likes Beginning Java and the fly likes Access modifiers for a class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Access modifiers for a class" Watch "Access modifiers for a class" New topic
Author

Access modifiers for a class

prathibha ananthapadmanabha
Greenhorn

Joined: Jul 12, 2007
Posts: 16
Hi All ,

Is it that in one java source file ( .java file ) there should be only one class with access modifier " public " or
a java source file can have as many " public " classes as possible ?

Please clarify ........

Regards,
Prathibha.
Christophe Verré
Marshal

Joined: Nov 24, 2005
Posts: 14361

Only one public class per file. (same as the filename)


[SCBCD Wall of Fame] [My Blog]
All roads lead to JavaRanch
Help Japan. Make a donation.
prathibha ananthapadmanabha
Greenhorn

Joined: Jul 12, 2007
Posts: 16
Hi Christophe verre ,

Thank you for clarification .

Regards,
Prathibha.
bala kris
Greenhorn

Joined: Jul 10, 2007
Posts: 5
hi prathibha,

In Java you can only have one public class... That too with the same file name... But you can have several inner class inside the main class...
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3287

  • A Java class can have any number of classes inside.
  • But there should be ONLY one public class
  • The Name of the public class should exactly match the name of the Java file.



  • HtH.


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Satya Maheshwari
    Ranch Hand

    Joined: Jan 01, 2007
    Posts: 359
    You can have inner classes to a class which are public. Hence in this way it is possible to have more than one public class in a single file. For e.g.


    SPOJ ROCKS!!!
    Peter Chase
    Ranch Hand

    Joined: Oct 30, 2001
    Posts: 1970
    Yes, the rule is that you must have a maximum of one top-level public class per source file, and that, if you have one, the source file name must be the same as the public class.

    A file can have as many top-level package-private classes as you like. However, I think it is bad practice to use this lattitude. I think that, even when classes are package-private, you should limit yourself to one top-level class per source file, which should be named after the class.

    As already mentioned, nested and inner classes have no restriction as to their number, even when public, and do not affect the source file name.


    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
    prathibha ananthapadmanabha
    Greenhorn

    Joined: Jul 12, 2007
    Posts: 16
    Dear All ,

    Thank you so much for the clarifications given .
    So to conclude , it is that one java source file must have only one public class with the same name as the name of the source file .


    Regards,
    Prathibha.
    Satya Maheshwari
    Ranch Hand

    Joined: Jan 01, 2007
    Posts: 359

    So to conclude , it is that one java source file must have only one public class with the same name as the name of the source file .


    Just a minor change:
    So to conclude , it is that one java source file must have only one top-level public class with the same name as the name of the source file .
    prathibha ananthapadmanabha
    Greenhorn

    Joined: Jul 12, 2007
    Posts: 16
    could you please clarify what it means by " top -level " public class , it means that other public classes are possible in a java source file .

    Regards,
    Prathibha.
    Satya Maheshwari
    Ranch Hand

    Joined: Jan 01, 2007
    Posts: 359
    could you please clarify what it means by " top -level " public class , it means that other public classes are possible in a java source file .


    By "Top-level", I meant class which is not an inner class. Inner-classes are the classes as members inside a top-level class. These inner-classes can be public along with their top-level class. Please see the example I quoted in my earlier post where this is done.
    prathibha ananthapadmanabha
    Greenhorn

    Joined: Jul 12, 2007
    Posts: 16
    Hi

    Now it is clear . Thank you for the clarification .

    Regards ,
    Prathibha.
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3287

    Thanks Satya Maheswari for providing the additional information about inner classes being public.
     
     
    subject: Access modifiers for a class
     
    developer file tools