Results 1 to 4 of 4
  1. #1
    kthaker is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Can we have a java program without any public class?

    Hi All,

    I would like to know that is it complusory to have atleast one class declared as public in our JAVA program ?. I mean will my program run without any error if anny of the class in my program is not declared as public. Please if anny one can give me this answer then I will be very thankfull.

    Kirtan

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    A class can only be private if it's an inner class. So all classes must be public if they are not nested within a class.

    Java Code:
    public class MyClass //must be public
    {
       ...
       private class MyInnerClass //can be public or private
       {
          ...
       }
       ...
    }
    Last edited by mrmatt1111; 10-05-2009 at 10:37 PM.
    My Hobby Project: LegacyClone

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by mrmatt1111 View Post
    A class can only be private if it's an inner class. So all classes must be public if they are not nested within a class.

    That doesn't sound right. What about classes that are neither public nor private?

    @OP: Have you tried answering your question by writing some code? Take "HelloWorld" and try it out with all the combinations of access modifier you can think of. What compiles? What runs?

  4. #4
    kthaker is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 10:17 AM
  2. Replies: 5
    Last Post: 06-26-2009, 04:03 AM
  3. Replies: 14
    Last Post: 05-28-2009, 06:44 AM
  4. different multiple public class and main class
    By mr idiot in forum New To Java
    Replies: 2
    Last Post: 01-01-2009, 01:10 PM
  5. Public class variable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •