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.

Similar Messages

  • 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 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 {   }
    }

  • 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());
    }

  • 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.

  • Job with Multiple Schedule of same time & only one is executed, Why

    Hi ALL,
    I've Created a package to notify report in email  as excel, which is using a path to render report and save it as excel.
    This package was need to be schedule every first day of month and on every Friday even at morning 7 AM.
    So logically these are two events.
    But if we see on 1 May 2015 we have Friday and plus first day of month
    So technically this should fail.
    either for IO operation for render &
    storing data over same location with same file name.
    To replicate this i created two three schedule
    Below is the job history, job is invoked by only one schedule
    Question-
    1) Why one one schedule execute?
    2) why "sched2" only , it could be "sched3" only or "DentalOperationStackAndRank-Sched1" Only?
    Another this i result into error if different job executing same package at same schedule.
    FYI- package is deployed at database level
    HS

    Hi HS,
    As described in this
    article, more than one job can run on the same schedule, and more than one schedule can apply to the same job.
    And I make a test about your scenario, when configuring a SQL Server job with three schedules that reference same dates and times, my job behaves as yours that only one schedule is executed, but the job is invoked by schedule2 or schedule 3 in my environment.
    It is a normal behavior of SQL Server job in my opinion. For more details about such scenario, you can track the job following this
    blog.
    However, when I configure three jobs that run on the same schedule, everything works well and the three jobs all run successfully.
     I recommend you configure three different jobs that execute same package at same schedule .
    Thanks,
    Lydia Zhang
    Lydia Zhang
    TechNet Community Support

  • 2 videos, same format, only one syncing.  Why?

    I have 2 home movies. One will sync and the other will not. I have tried converting the file 1 billion times using MPEG Streamclip and Handbrake. Nothing works. They are both the very same format. Both are MPEG-4, 720x576, H.264, AAC. So, why will one sync and not the other?

    Which default setting are you using for the conversion with HandBrake?
    Since I have an Apple TV, I use the Apple TV default setting which also allows for transferring the video to my iPhone.

  • Copy down - only one line (mac)  - why?

    I'm using the dynamic form wizard and when I'm trying to copy down a value for many posts I only get it on the next line, not on every one.
    I'm on a mac - for the first time. It's always worked on Win.
    Anyone know a recipe for it??

    I tested on Windows and it doesn't work there either.
    It's quite a long time since I used it, but then it was possible to copy down text fields on several rows. Now I seem to have to use menus.
    I wonder if it's something I've missed...

  • Public class question

    I want to know what is the logic behind saving only one public class in one .java file. and why public class is saved with the same name.
    thanks for consideration

    If you're new to a large project with many classes and your assigned to make a modification to class X then where do you find this code.
    If you know that class X is in the file X.java then its easy to find.
    C++ is a pain for this, IDE's like visual studio will normally follow the same rules but allows the programmer to break those rules if he wishes.
    Java is stricter keeping code more consistent.

  • Public class and the source filename

    May be I'm asking a silly question, but why the name of the source file needs to match with the public class's name?
    If the rule is that only one public class is allowed per source file, then the compiler can simply flag the error and quit.
    Any reasons behind this, other than for the ease of compilation?

    I refer you to the Java Language Spec:
    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.

  • Question about access non-public class from other package.

    Hi, everyone!
    Suppose class A and class B are in the same java file of package pkg1
    -- A.java. So, A is a public class and B is a non-public class.
    If I want to access class B from another class class C and class
    C is in package pkg2. When compiling, an error occurs indicating
    that class B is not visible to class C.
    So, If I defined serveral classes in one java file and I want to
    access every class from other package. How should I do?
    (I think in one java file, there should be only one public class and
    only the public class can be accessed from other package.)
    Thanks in advance,
    George

    So, If I defined serveral classes in one java file and
    I want to
    access every class from other package. How should I
    do? As you already seem to know, there is at most one public class allowed per source file (at least, with javac and most popular compilers). So if you want more than one public class, you will need to use more than one file...

  • Why it is necessary that public class name should  same as filename in java

    hi,
    why it is necessary that public class name should same as filename in java.
    As I know that it is a convention, but I want the information why this convention is made.
    thanx.

    user13445117 wrote:
    but suppose if i am creating a java file with default access modifire then we do not need to save the file name as a class name.So, your question is "Why does the rule about top-level classes being in a file of the same name apply +only+ to public classes"?
    If it were applied to all classes, then we could never have two top-level classes in the same file. Doing that is generally not a good idea, but it might be useful from time to time. EJP already told you why it applies to public classes, and, since these are the most common by far, it's a fair compromise that it apply to them but not to other classes.
    Was this the reason the original designers did it that way 15 or so years ago? Don't know, don't care, but it's one plausible explanation. Maybe it was just a balance between simplicity and clarity on the one hand, and flexibility on the other.
    It's certainly not something I can see caring that much about, unless you're doing an in-depth history on Java, or writing an in-depth treatise on computer language and compiler theory.

  • Two public classes in one source file

    Can anyone please explain what is the exact reason why the java source file name should be same as the only allowed public class name in the source file. Answer only if you know the correct answer. No gusses please. I read the other postings on this topic. None of the answers were correct.

    One reason is that some RISC processor architectures
    (like those Sun uses mostly) have hierarchical memory
    architectures. This hierarchy is divided into global
    modules and local (global-accessible) submodules. The
    advantage of this is that the processor needs lesser
    memory access "points" (handles), as the modules
    delegate CPU calls to the submodules.
    The java compiler can utilize this and increase
    performance by loading the entire source files into
    the memory and assign them a CPU handle (the module
    ID). It'll be later used for linking, e.g. The public
    class in a file will be loaded as a module and gets
    the handle, all other non-public classes in that file
    will be submodules.
    If there is no public class, a generic module will be
    used, so that's no problem. But if there are multiple
    public classes, you'd end up with several modules and
    just one handle to assign, thus having ambiguity. The
    CPU won't be able to address the correct module.
    This all only applies to RISC CPUs, but for obviuos
    cross-platform compatibility reasons it was added to
    the standard - it doesn't hurt the other
    architectures, but helps those with hierarchical
    memory management.Thanks, but the rooster explanation makes more sense to me.
    Now could someone please express it a la Majinda?

  • I can select only one row in my table why?

    hi.. i have just started cold fusion. i am trying to connect
    my database. its ok. but i can see only one row? why?
    ff4.cfm =>
    <cfquery name = "myFirstSelect" datasource="calisma">
    select * from fuat1
    </cfquery>
    ff5.cfm =>
    <cfinclude template="ff4.cfm">
    <table>
    <cfoutput query="myFirstSelect">
    <tr height="20" onMouseOver="this.bgColor='#colorlist#';"
    onMouseOut="this.bgColor='#colorrow#';" bgcolor="#colorrow#">
    <td>AD</td>
    <td>SOYAD</td>
    <td>NUMBER</td>
    </tr>
    <tr height="20" onMouseOver="this.bgColor='#colorlist#';"
    onMouseOut="this.bgColor='#colorrow#';" bgcolor="#colorrow#">
    <td>#AD#</td>
    <td>#SOYAD#</td>
    <td>#NUMBER#</td>
    </tr>
    </table>
    </cfoutput>
    i can retrieve only one row. Cannot access the second row of
    table. What can I do? What can I reach the second row?
    thankx

    ok i solve this problem thanx

Maybe you are looking for

  • MBP display won't stay off, wakes from sleep

    I have a newish 15" MBP, 1680 display, hooked up to a Dell external monitor when it's at home. When I go to bed I cut power to the display via control-shift-eject, but generally don't put the computer to sleep overnight. With this new machine, after

  • Import Multiple Records to a PDF Form

    Hi, I produce a separate XML file for every voucher sent to a client on a golf tour.  Using a form created in LiveCycle Designer 8.0 I create a corresponding PDF document for each voucher and import the data using Acrobat XI Pro. However I need a bet

  • How to add audiotracks so they can be selected by the viewer?

    I am using iMovies 11. Its really easy to use but i still got a question about audio. My goal is, to allow viewers to switch between the original recorded audio, a music track and if they want, a overlay of both. If i got it right, there must be thre

  • J2SDK 1.2.1_02 on RH 8.0

    I am trying to port my app from windows to linux. I just installed the J2SDK on a full install of RH 8.0 and I am getting the following error message. HelloWorldApp.java:3: package Systems does not exist Systems.out.println("Hello World!"); ^ 1 error

  • Personalize a page template

    Hi, by creating my pages I' usign a predefined template. How is posible to sobstitute the "CompanyName/Logo" on the bottom with the custom ones? Thanks in advance. Renato.