• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access modifiers for a class  RSS feed

 
prathibha ananthapadmanabha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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é
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one public class per file. (same as the filename)
 
prathibha ananthapadmanabha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe verre ,

Thank you for clarification .

Regards,
Prathibha.
 
bala kris
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • 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.
     
    Satya Maheshwari
    Ranch Hand
    Posts: 368
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Peter Chase
    Ranch Hand
    Posts: 1970
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    prathibha ananthapadmanabha
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 368
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 368
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi

    Now it is clear . Thank you for the clarification .

    Regards ,
    Prathibha.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Satya Maheswari for providing the additional information about inner classes being public.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!