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.

Similar Messages

  • Why public class name should be same as the java file name

    Hi,
    I would like to know, why public class name should be same as the its java file name. Iam in the process of finding the answer to this question. Can someone help me out in finding the explanation.
    Thanks in advance,
    Manoj

    This is a requirement of the Java reference compiler released by Sun. I have used compilers that did not require this, but most seem to follow the reference compiler (which is a very good idea). I am NOT sure if this is specified in the Java Language Specification. Some of the other regulars who are more familiar with the JLS than I may be able to tell you.
    ? {?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Public class name same as file name

    Why should the public class name be same as the file name in which it is written?
    http://forum.java.sun.com/thread.jspa?threadID=198585&messageID=657978
    The above thread says it is because it makes the compilation faster. is that the only reason?
    Thank you.

    That's specified in the JLS. See � 7.6, Top Level Type Declarations.
    <i>
    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. <b>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.</b>
    When packages are stored in a database (�7.2.2), the host system must not
    impose such restrictions.
    </i>

  • Why File Name is Same Name as The Public Class Name

    Hi Friends,
    What is the reason for Naming the File Name is puvlic class name??
    thanks in advance.

    konstkaras wrote:
    What could be reason to give it other name?
    It's as much better as we have less distinct names.
    When I was a schoolboy, I programmed Turbo Pascal. Each program there had a name (declared in file's beginning). The name was never used, but: it has no such restriction of length (as MS-DOS file names had) and it could clash with other identifiers described in code.
    So why those names should be distinct?Java was invented after MSDOS was obsolete, so 8.3 limits were not a consideration.
    Java is designed for larger programs than were attempted in the MSDOS era, so supports compilation of programs which are created from many source files. Turbo Pascal 4 also supported separate compilation units, and did use the name you mention for exactly that purpose, but I can't remember how it dealt with dependencies between compilation units.
    Having the public class' name and package as component in the source file path and the class file path simplifies the dependency problem between the compilation units - all the compiler has to do to determine whether the foo.bar.Baz class needs updating is to look whether foo/bar/Baz.class exists, and if it is less recently modified than foo/bar/Baz.java.

  • Public class must be written in a seperate .java file?

    Hi,
    1. why is it compulsory for a public class to be written in a seperate .java source file?
    2. why can not a .java source file contain more than one public classes?

    Can you elaborate more on "to make it quicker for the compiler to import packages"Think what you'd do if you were the compiler and you needed to handle this code :
    package my;
    import my.gaphics.*;
    import my.util.*;
    class My {
      private Utility x;
    }How do you work out where to look for the source of class Utility? If you insist that all public classes are declared in a file with the matching name then you only need to check whether my/graphics/Utility.java or my/util/Utility.java exists, and there you have your source. Otherwise you'd need to parse all imported packages upfornt, since you can't tell which file will contain the source of Utility.

  • "why the Psoft oracle DB and SID names should be in all upper case

    Customer is having a Question like
    "why the Psoft oracle DB and SID names should be in all upper case (according to the PsoftTools 8.50 Installation for Oracle manual)
    why the Peopletools 8.5 install book states all Oracle SID and database names should be in UpperCase.
    If we use lower case database name, would encounter any problems, what sort of problems?
    i Observed that all the Installation manual for 8.49,50,51 States the Same for Creating of Data base !
    Task 1-6-1: Determining Databases and Database Names
    You should also determine the names of the
    databases at this point, using database names that:
    • Are limited to eight characters, all UPPERCASE.
    • Capture information about the PeopleSoft product line and the type of database
    Please suggest on this !
    Thanks
    Bala

    user13798675 wrote:
    Customer is having a Question like
    "why the Psoft oracle DB and SID names should be in all upper case (according to the PsoftTools 8.50 Installation for Oracle manual)
    why the Peopletools 8.5 install book states all Oracle SID and database names should be in UpperCase.
    If we use lower case database name, would encounter any problems, what sort of problems?
    i Observed that all the Installation manual for 8.49,50,51 States the Same for Creating of Data base !
    Task 1-6-1: Determining Databases and Database Names
    You should also determine the names of the
    databases at this point, using database names that:
    • Are limited to eight characters, all UPPERCASE.
    • Capture information about the PeopleSoft product line and the type of database
    Please suggest on this !
    Thanks
    Balabcm@bcm-laptop:~$ env | grep ORA
    ORACLE_SID=v112
    ORACLE_BASE=/u01/app/oracle
    ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
    bcm@bcm-laptop:~$

  • Proxy class name should be same in all env. Correct ?

    Hi Friends,
    We have ECC development, quality and production system.  We already did the proxy settings in ECC dev and it is connected to PI. It is fine.
    In SPROXY, we created class for our outbound interface in ECC dev system. While the class is created we saved this in package (to be transported to ECC quality).
    We will do SPROXY config in ECC quality system to connect to PI.
    PI system is same for both development ECC and quality ECC.
    Now, my doubt is, 1) do we need to create again class through SPROXY in ECC qulaity for the same OB interface ?
                                 2) If it is 'yes', is it required to transport the class to from ECC dev to ECC quality ?
    My understanding is a) we need to create class in quality ecc (SPROXY). b) The class name should be same as in ecc dev 3) It need not required to transport the class from ecc DEV to quality since we did not do anything for the class.
    Can you kindly clarify friends.
    Kind regards,
    Jegathees P.

    I do not get your points.
    > I agreed your point.  My query is once the proxy class transported into QUALITY, how the call/execution will happen ?
    The same way as in dev.
    > It means how it understands it needs send data to XI system when this class is called through Z abap program in Quality.
    Yes.
    > How this is differentiated from normal class call & proxy class call?
    There is no difference. You call the proxy class the same way as any other class that you create with SE24.
    The only difference is, that this class is generated.
    So the whole transport mechanism applies for the ABAP proxy as well.
    In our system I put ABAP proxy and calling program in same package and transport them together. But this is up to you.

  • Can we define two public classes in a same file?

    Is it possible to define two public classes in a same file
    Thanks a lot
    So jag

    Do you realize RMI too, what caused this please!
    loading chat.rmi.RMIServer.class for debugging...
    chat.rmi.RMIServer.class successfully loaded
    java.rmi.StubNotFoundException: Stub class not found: chat.rmi.RMIServer$RemoteLogonManager_Stub; nested exception is:
    java.lang.ClassNotFoundException: chat.rmi.RMIServer$RemoteLogonManager_Stub
    java.lang.ClassNotFoundException: chat.rmi.RMIServer$RemoteLogonManager_Stub
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:191)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:296)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
    at sun.rmi.server.RemoteProxy.loadClassFromClass(RemoteProxy.java:198)
    at sun.rmi.server.RemoteProxy.getStub(RemoteProxy.java:100)
    at sun.rmi.server.RemoteProxy.getStub(RemoteProxy.java:62)
    at sun.rmi.server.UnicastServerRef.setSkeleton(UnicastServerRef.java:155)
    at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:115)
    at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:102)
    at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:276)
    at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:180)
    at java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:80)
    at java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:67)
    at chat.rmi.RMIServer$RemoteLogonManager.<init>(RMIServer.java:23)
    at chat.rmi.RMIServer.<init>(RMIServer.java:16)
    at chat.rmi.RMIServer.main(RMIServer.java:11)
    at symantec.tools.debug.Agent.runMain(Native Method)
    at symantec.tools.debug.MainThread.run(Unknown Source)
    The communications channel closed.

  • Why the constructor of the base class calls first when u run the java app.

    why the constructor of the base class calls first when u run the java application

    For the record the other very exciting questions are:
    pls give the differences between application server and web server with examples
    will u pls narrate the differences between servlet context and servlet config.....
    where can we find servlet config...
    is there any methods to access servlet config

  • Why a public class name and file name same?

    All of us know that, a public class should be declared in a file which has the same name of that class. What is the underlying concept behind this..Can someone give a clear explanation?
    Thanx..
    Sandeep Joseph
    Paragon Solutions
    [email protected]

    Another issue, it was said that the file must be named
    after the class to find the class's code when
    compiling dependent classes. Doesn't this apply to
    non-public classes too (with the dependent class being
    located in the same package)?This applies to all top-level classes ...
    You can define multiple classes in one file though:
    class A {
    class B {
    }It's perfectly valid to have both classes defined in the same file (named A.java if A is the first class to be defined in that file).
    You will get a compiler warning as soon as you reference a non-top-level class from a class that is defined in another file, though.

  • Why the name of our source file should be same as the 'public class'  name

    Hi all,
    I am very new in java and have a question in my mind.
    I read somewhere , our souce file name should be same as public class in that file.
    but i don't know the reason for this.
    Can anybody help me out with this .
    Thanks,

    HMRPanchal wrote:
    Thanks,
    Can you give me some link or documents from where I can go through this article.
    because I am not clear with what you are saying( makefiles , automatic recompilation etc).
    I am a new in Java .The fact that you don't have to worry about those things is what makes it A Good Thing.

  • Public class name same as filename

    Every book I've read stated that if you have a public class declared in the source file, then you have to name the file the same as the class name. However, none of the book explain why, can anyone tell me about this?

    Not all public classes need to have the same name as the file that contains them -- only top-level public classes. Public nested classes can have a name that's different from the filename.
    Also, not only do top-level public classes need to have the same name as the file that contains them, but the names of the package and the directory structure must match as well.
    This is done simply so that if you see a class named com.mycompany.mypackage.MyClass referred to somewhere, you know that the source code is in the file com/mycompany/mypackage/MyClass.java. Isn't that helpful? ;-)

  • Why MetalSplitPaneDivider is not a public class?

    Dear All,
    The MetalSplitPaneDivider is not a public class because of that, I am not able to extend it to give a custom implementation. My requirement is that, whenever the leftButton is pressed, it should not go the 0 position, instead I have to have a control over this. But since, MetalSplitPaneDivider is not public I am not able to do this. I could achieve the same in BasicSplitPaneDivider, MotifSplitPaneDivider and WindowsSplitPaneDivider. Is there any way by which I could achieve this in MetalSplitPaneDivider? Is this a bug or an intended one?
    Awaiting for your valuable respones. Thanks in advance!
    Best Regards,
    Amudhan.

    Are you trying to accomplish the same effect as JSplitPane.setOneTouchExpandable( false ) ?

  • Why is it necessary that, every time I open Firefox to Google, it displays the Search | Firefox Help" tab and the "Start Now" tab. These are redundant and not wanted. How can I eliminate them?

    I don't know what more details could be needed to respond to this simple question. It is a nuisance to have the help window when there is a help tab and "Start Now" may be some people favorit search engine but not mine.

    Try the Firefox SafeMode. <br />
    ''A troubleshooting mode, which disables most Add-ons.'' <br />
    ''(If you're not using it, switch to the Default Theme.)''
    # You can open the Firefox 4.0 SafeMode by holding the '''Shft''' key when you use the Firefox desktop or Start menu shortcut.
    # Or use the Help menu item, click on '''Restart with Add-ons Disabled...''' while Firefox is running. <br />
    ''To exit the Firefox Safe Mode, just close Firefox and wait a few seconds before using the Firefox shortcut (without the Shft key) to open it again.''
    If it is good in the Firefox SafeMode, your problem is probably caused by an extension, and you need to figure out which one. <br />
    http://support.mozilla.com/en-US/kb/troubleshooting+extensions+and+themes

  • Class name and file name

    if i have putiple classes in a single file then if a clas declared as public then the file name shoud me same as class name.... why?
    if i declare two public class in a same source fiel what will happen????????
    plz help................
    thanks in advance

    And i bet a million dollars that even you don't
    search the forum before posting anything . So stop
    suggesting that .Too bad that I wasn't included in the bet. You won't be coz you are a veteran and it's from you and the other veterans(I will include my peers as well) that i learnt a lot including good googling.
    It happens
    that I search the forum before I post an answer.
    Searching the forum using google is actually very
    effective.Yes and thats the very reason i didn't mention googling in that reply because i know that google gives the results from the threads in the forum as i myself have read a lot of posts at this forum using google and obtained solutions.But it's a different case when a person has to manually search the forum(without google) to see if a topic matches his needs and as i just mentioned off lately most of the posts come out with crappy headers like "Please Help Me" ; "Error" ; "Can't compile" ; "Help Needed" ; "What's wrong in this".How much sense does these headers make with respect to the what the post is really about.One can't expect a person to verify each such post with such crappy headers and read each's contentns.Just imagine how frustrating it is.Even the posters lose their cool when someone posts with such titles.Then just imagine what will be the condition of the person who is asked to check the forum before posting.But i totally endorse googling.There is no pardon or any sort of respite for a person who has posted without googling which would have fetched him top notch answers.
    Oh God!That looks really long!

Maybe you are looking for

  • Memory module not recognized on K7T266 Pro2

    Hello.  I have a computer with only 256 Mb of RAM and I wanted to add 256 Mb but my second memory module of 256 Mo is not recognized whereas it  functions very wel.  Here are details :  - Mother board : MSI K7T266 Pro2 (ms-6380 v2.0) with 3 memory sl

  • Apex 4.1 different font text in Reports Region and Interactive reporting

    Hi everybody, In my APEX 4.1 page, I have two regions using the same ''Reports region" Template but font text are different. Why The first region is an interactive reporting. The second it's a report. I know that each one has his own data for sure bu

  • PO details

    hye, when i make a po, i must get the header items like - Purchasing orgnization, company code, order currency, purchasing group, Payment terms, to appear Where do i make that as mandatory if not for me21n - then if i select the document type ZNB (sp

  • Configuring Library Path in Application Server Control

    Hello Everyone, I am using : 1. HP UX B.11.23 64 bit OS 2. Application server 10.1.3.1 3. 64-bit JVM (JRE 1.5.0_03) I am facing a problem with the connection pools in 10g EM Application Server Control. Initially the connection pool used JDBC thin cli

  • Application

    Hi guyz, i have an assets application developed in Oracle 10G forms using 9i database. Office guyz can access application, I have an real IP is there anyway that some other branches can access the same application through net? if yes anyone guide me