Why can we have only one public class in our program

Why is it that we can specify only one class as public in our source code .................................................
Once i'd put one inner class as public ...which means now i have 2 public classes ..............out of which one is the inner class...what is the use of such public class which can be accessed by only its outer class.........
plz. help

Once i'd put one inner class as public ...which means
now i have 2 public classes ..............out of
which one is the inner class...what is the use of
such public class which can be accessed by only its
outer class.........
plz. helpWrong.
Public inner class can certainly be accessed outside.
You just have to preface it with the name of the outer class.
A.java
=====
public class A {
   AA example = new AA();
   public class AA {
       public int value() { return 5; }
}B.java
=====
public class B {
   public static void main(String[] args) {
      A x = new A ();
      A.AA y = x.example;
      System.out.println(y.value());
}

Similar Messages

  • Why we can have only one public class in one source file

    why we can have only one public class in one source file

    When the java compiler is run it looks for classes referenced by the classes you are directly compiling. When looking for these referenced classes it looks for source as well as class files and compiles them automatically if the coresponding class files are missing, or older than the source.
    In order to do this it must be able to work out the source file name for any given class which might be referenced from another (the rule also applies to package level access).

  • Why only one public class in one file

    why does java allows only one public class in one file?
    Why can not we have two or more public classes in file?
    Thank u.

    Note, you can have multiple inner classes.
    e.g.
    public class A {
        public static class B {   }
        public class C {   }
        private class D {   }
    }

  • Only one public class - why???

    Hi
    I have come across this statement many places -
    "There can be only one Top-Level 'public' class in
    a java source file which should have same name as that
    of the fore mentioned class."
    I know that the compiler searches for the class
    with same name as that of the file that is passed to
    the compiler.I am also aware that a Java source compiles
    smoothly with One or None Top-Level 'public' class,
    but I fail to comprehend this -
    Why can there be only one TOP-LEVEL 'public' class
    in a Java Source file?
    The importance of 'TOP-LEVEL' is as important as
    any thing else, as you yourself check that a Nested
    class within an Enclosing class can be 'public' along
    with someother Top-Level 'public' class in the program
    compiles smoothly unlike having TWO Top-Level 'public'
    classes in same file.
    I have found no reason supporting this statement in any
    of the refernces I have checked out.Ppl I have asked told
    me that it is so coz Java Spec say so.
    Is there an better answer to my question?
    Thanx in advance, appretiate it.
    Regards
    Pradeepto

    I have found no reason supporting this statement in
    in any
    of the refernces I have checked out.Ppl I have asked
    told
    me that it is so coz Java Spec say so.I can tell you that anybody who says its because the Java spec says so, is wrong - the Java spec does not say so.
    It is a limitation of Sun's javac compiler, which many other compilers exhibit as well. I don't know the full formal reasoning behind it (and would be interested in knowing if you find a good answer), but I assume it simply has to do with finding classes without having to load everything on your classpath.
    Whether other compilers are purposely acting the same way as javac, or whether it is some sort of performance optimisation that most vendors feel is worthwhile, I couldn't say.
    I don't see what you're saying about top-level and nested classes. Although you can declare a public nested class inside a non-public top-level class, the nested class is not actually publicly visible, by virue of its enclosing class not being publicly visible.

  • Why only one public class in a source file?

    why we have to write only one public class in a source file?

    PhHein wrote:
    Because the JLS says so.It does?
    From section 7.6 of the JLS (third edition):
    When packages are stored in a file system (§7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:* The type is referred to by code in other compilation units of the package in which the type is declared.
    * The type is declared public (and therefore is potentially accessible from code in other packages).
    This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.
    >
    To my mind, it's strongly recommending, rather than mandating, this practice. All academic of course, merely shifting the answer from "Because the JLS says so" to "Because pretty much all Java compilers say so". I don't know of any that don't enforce this, but of course, that doesn't mean they don't exist

  • Why can i see only one picture in the illustration windows with iTunes 10.5.1?

    why can i see only one picture in the illustration windows since iTunes 10.5.1?

    Assuming Apple doesn't eviscerate the files of the secondary graphics with another upgrade, there could be a plug in that reads the graphic and runs along side iTunes..we should get someone or Apple to program an alternative If we have to accept this downgrade in iTunes proper.
    I've developed a quick workaround, and am still hoping Apple will fix this!
    Go To Get Info on the tune you want to see secondary artwork for.
    Go To the Artwork tab
    Drag the back cover to the desktop, and it will create a new jpg you can open with quick viw or prieview where you can see it large!
    you end up with a lot of jpgs on the desktop to discard!

  • Why can I burn only one CD from a playlist?

    After creating a playlist of Itunes, I was able to burn only one CD.  Other attempts would not play in auto CD player.

    Figured this one out on my own. (duh)  I had playlist open but failed to "select all" before burning.  Once all tracks were selected the burn was successful.

  • I have one IMac and two Ipod and two ipad for the hole familie. We had all people a separatly ITunes Account. The ITunes said this Imac is for 90 Days closed. Why ? we have only one Imac!

    I have e big problem with my ITunes and my mobile devices. Two Ipod Touch  and two IPad.
    My son and my wive und myself have e seperatly ITunes Account. My Account ist with credit card my son and my wive only with Itunes cards.
    All devices are protected by my Mac with other accounts in each case and are administered. Now the Imac has switched off the tone and said one cannot div. Accounts join! Nevertheless, this cannot be. This would be a complete nonsense! What can i do that all Devies woks?
    Thanks for your help

    Have a look here...
    http://macmost.com/setting-up-multiple-ios-devices-for-messages-and-facetime.htm l

  • Can I have only one library for me & my wife?

    I created it originally and have tried dragging my iphoto library into the shared folder, but while I can then access the photos fine from there, my wife sees it as a locked folder (and even more odd, only half of the folders of photos (years) are locked). I can't unlock them for her to access no matter what I try. Any advice?
    I should clarify that, of course, we are two separate user accounts.

    Hi tom,
    here is all the info and links I have on the library sharing. the newest info is the one on using ACL's (the last link)
    SHARING IPHOTO LIBRARIES
    Sharing libraries between users on the same local network
    You can also share a library on the same machine between users the same way.
    Sharing libraries between users on the same machine...
    -both users must have sharing enabled in their iPhoto Preferences
    -both users must be logged in and have iPhoto running
    -Remember that you can only view the other library. You can't edit the photos or play the saved slideshows or view the made books or burn them to CD or DVD. The books and slideshows will show up as an album. You can play that album as an "on the fly" slideshow. You can drag images from the sharing library to your library in the source column to import them to your library. You can then edit, add to albums, books, slideshow, etc.
    Sharing links from Apple....
    Sharing your photos between computers
    About shared photos
    Turning off photo sharing
    Looking for shared photos
    SHARING ONE IPHOTO LIBRARY BETWEEN USERS ON SAME MACHINE
    You can try one of these three methods:
    1- Use iPhoto Library Manager-the paid version
    The documentation page will give instructions on how it is done.
    2- Sharing one iPhoto library between several users on one machine
    3- Share an iPhoto Library in tiger Using ACL's

  • Can Linda have only one Apple ID for 2 devices?

    Linda has an iPod that she says she has an ID for. When she gets home today she will be setting up her new iPhone 4s.  Should she make an entirely different ID for the iPhone?

    Would you need help to learn how to have the apps on Linda's first device on her second one? The apps will not be there when you log in on the second device.

  • Why only 1 public class in java file

    In any java file, why do we have only one public class whose name is same as the java file name?

    Jasmit1986 wrote:
    Thanx for the link db. But in the link it is explained why we have the name of the java file same as the class name.
    My doubt is that why can't we have more than one public class in a java fileTo keep things simple and less confusing. There's really no good reason to have multiple public classes in one file, so this just enforces the "best practices" idea.

  • Why a table can have only one PK ?

    Just wanted to know the reason why Oracle allows only one PK per table ?
    I came across a situation where my existing large PK which is on a single (ID) column needs an alteration to add a new column (No).
    ID column on which PK is based is now subjected to have duplication and that is the reason why i want to add a new column. The new column composition is not going to ensure uniqueness.
    So, i need to create (alter) PK on two columns now.
    It seems, in order to do that, i need to drop the existing PK and create a new one. Since this is a OLTP production database, business cannot afford to drop the existing pk.
    When i tried to create a separate PK on two columns, Oracle gave me below err:
    ORA-02260: table can have only one primary keySo, i wanted to understand why Oracle allows only one PK / table ? and considering above err, is there any way to make required changes to my PK without altering existing PK?
    (Table is a large 30 GB partition table and PK is a non partitioned global index)
    I think PROBABLY i need to drop existing PK and re-create the new one. But core intention for opening this thread is to know rational behind one pk/table concept.

    OraDBA02 wrote:
    Just wanted to know the reason why Oracle allows only one PK per table ?
    It's not exactly Oracle's fault. The ISO SQL standard requires that a "PRIMARY KEY" constraint can only be used once per table. However, a UNIQUE constraint on non-nullable columns achieves effectively the same thing as a PRIMARY KEY. You can have multiple UNIQUE constraints.
    Arguably the PRIMARY KEY constraint is unnecessary and could safely be dropped from the SQL language. In principle in the relational model a "primary" key is nothing more or less than a candidate key. All keys are equal in the relational model and there is no fundamental reason to designate a primary key at all. There is certainly no reason not to have multiple keys. In the SQL world though it is a very widely observed convention to use a PRIMARY KEY constraint for each table and to use it for all foreign key references.

  • HT204053 i RECENTLY PURCHASED ANOTHER IPHONE.  MY INTERNET IS NOT WORKING.  I JUST CREATED AN APPLE ID.  WHAT'S NEXT?  I NOW HAVE TWO DIFFERENT ACCOUNTS WITH DIFFERENT USER NAMES.  HOW CAN I USE ONLY ONE ACCOUNT FOR ITUNES, ICLOUD APPLE ID ETC???

    I RECENTLY PURCHASED ANOTHER IPHONE.  MY INTERNET IS NOT WORKING.  I JUST CREATED AN APPLE ID.  WHAT'S NEXT?  I NOW HAVE TWO DIFFERENT ACCOUNTS WITH DIFFERENT USER NAMES.  HOW CAN I USE ONLY ONE ACCOUNT FOR ITUNES, ICLOUD APPLE ID ETC???

    Welcome to the Apple community.
    iTunes and iCloud and different accounts, you will need to delete both accounts from your device before adding the new details in their place.
    For iCloud go to settings > iCloud, scroll down and hit the delete button. You can then sign back in using your correct details. For iTunes go to settings >store, tap your account ID and then sign out, you can then sign back in using your correct Apple ID.

  • TS3276 I can have only one recipient at a time on e-mails I am sending.

    I can have only one recipient at a time on e-mails I am sending.

    Any ideas on how to fix it?

  • How i can have only one Apple ID for everything my computer and tab and iphone?

    How I can have only one Apple ID for evrything, my computer Iphone

    Thats how the majority of people use iCloud. All you need to do is sign into the same account on all of your devices.

Maybe you are looking for