Why class name must be same as its file name?

just curious

http://forum.java.sun.com/thread.jspa?forumID=54&threadID=613486
http://forum.java.sun.com/thread.jspa?forumID=256&threadID=452360
http://forum.java.sun.com/thread.jspa?forumID=513&threadID=516940

Similar Messages

  • Rmic Public class com must be defined in a file called "com.java".

    Hello,
    After compiling with javac which works perfectly, rmic fails with the message below. I have added the -verbose flag in an attempt to make it more clear to me however, the error does't make sense, why would rmic complain about a missing '{' where javac wouldn't? Any thoughts on this would be greatly appreciated.
    [yourabi@happyending java]$ rmic -classpath $CLASSPATH:. -verbose -keepgenerated -nowrite com/jeteye/lucene/DistributedSearchFacade
    [loaded ./com/jeteye/lucene/DistributedSearchFacade.class in 22 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/rmi/server/UnicastRemoteObject.class) in 2 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/rmi/server/RemoteServer.class) in 0 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/rmi/server/RemoteObject.class) in 2 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/lang/Object.class) in 1 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/rmi/Remote.class) in 0 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/io/Serializable.class) in 0 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/lang/CloneNotSupportedException.class) in 0 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/lang/Exception.class) in 0 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/lang/Throwable.class) in 3 ms]
    [loaded ./com/jeteye/lucene/IRemoteIndex.class in 0 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/rmi/RemoteException.class) in 1 ms]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/io/IOException.class) in 4 ms]
    [found remote interface: com.jeteye.lucene.IRemoteIndex]
    [found remote interface: java.rmi.Remote]
    [string used for method hash: "index(Lcom/jeteye/domain/Jetpak;)V"]
    [string used for method hash: "deleteByAlias(Ljava/lang/String;)V"]
    [string used for method hash: "deleteByJPID(Ljava/lang/String;)V"]
    [string used for method hash: "dateSinceOptimized()Ljava/util/Date;"]
    [string used for method hash: "transactionsSinceOptimized()I"]
    [string used for method hash: "numTransactions()I"]
    [string used for method hash: "startp()Z"]
    [string used for method hash: "shutdown()Z"]
    [string used for method hash: "isStarted()Z"]
    [string used for method hash: "isShutdown()Z"]
    [string used for method hash: "optimize()Z"]
    [found remote method <0>: java.util.Date dateSinceOptimized() throws java.rmi.RemoteException]
    [found remote method <1>: void deleteByAlias(java.lang.String) throws java.rmi.RemoteException]
    [found remote method <2>: void deleteByJPID(java.lang.String) throws java.rmi.RemoteException]
    [found remote method <3>: void index(com.jeteye.domain.Jetpak) throws java.rmi.RemoteException]
    [found remote method <4>: boolean isShutdown() throws java.rmi.RemoteException]
    [found remote method <5>: boolean isStarted() throws java.rmi.RemoteException]
    [found remote method <6>: int numTransactions() throws java.rmi.RemoteException]
    [found remote method <7>: boolean optimize() throws java.rmi.RemoteException]
    [found remote method <8>: boolean shutdown() throws java.rmi.RemoteException]
    [found remote method <9>: boolean startp() throws java.rmi.RemoteException]
    [found remote method <10>: int transactionsSinceOptimized() throws java.rmi.RemoteException]
    [loaded /usr/local/java/jdk1.5.0_04/jre/lib/rt.jar(java/lang/RuntimeException.class) in 1 ms]
    [wrote /home/yourabi/workspace/JetEye-HEAD/main/src/java/com/jeteye/lucene/DistributedSearchFacade_Stub.java]
    [parsed /home/yourabi/workspace/JetEye-HEAD/main/src/java/com/jeteye/lucene/DistributedSearchFacade_Stub.java in 70 ms]
    /home/yourabi/workspace/JetEye-HEAD/main/src/java/com/jeteye/lucene/DistributedSearchFacade_Stub.java:4: '{' expected.
    public final class com/jeteye/lucene/DistributedSearchFacade_Stub
    ^
    [checking class com]
    /home/yourabi/workspace/JetEye-HEAD/main/src/java/com/jeteye/lucene/DistributedSearchFacade_Stub.java:4: Public class com must be defined in a file called "com.java".
    public final class com/jeteye/lucene/DistributedSearchFacade_Stub
    ^
    2 errors
    [done in 449 ms]

    [yourabi@happyending java]$ rmic -classpath
    $CLASSPATH:. -verbose -keepgenerated -nowrite
    com/jeteye/lucene/DistributedSearchFacadermic needs the class name not the file name:
    rmic -classpath $CLASSPATH:. -verbose -keepgenerated -nowrite com.jeteye.lucene.DistributedSearchFacade

  • Why constructor's name is same as its class name?

    I would like to know why constructor's name is same as its class name.
    Any idea...

    It's because by means of the constructor, you can "construct" objects of the
    same class (by "same class" read "same properties") with values that will
    accomodate your needs for the work needed to be done.
    For example:
       MyColor redColor = new MyColor("RED");
       MyColor greenColor = new MyColor("GREEN");
       MyColor pinkColor = new MyColor("PINK");The class "MyColor" is unique in the JVM. However it has 3 instances, created by its constructor which, minimally, would look like:
        class MyColor{
             String theColor = null;
             //Constructor
             public MyColor(String whichColor){
                      theColor = whichColor;      
       }With such a constructor, you're telling the JVM:
    "Load the class MyColor and create an instance of it with the given values for its properties".

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

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

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

  • Why java file name and class name are equal

    could u explain why java file name and class name are equal in java

    The relevant section of the JLS (?7.6):
    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.
    When packages are stored in a database (?7.2.2), the host system must not impose such restrictions. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.

  • 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? ;-)

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

  • File name==class name. why?

    Why the name of the file must be the same of that of the public class it contains?
    And why is there a restriction that only one public class file must be pesent in a file ???

    Why the name of the file must be the same of that of
    the public class it contains?
    And why is there a restriction that only one public
    class file must be pesent in a file ???It's not a requirement of the Java language itself. This restriction is imposed by the Java tools such as compilers. It was introduced at a time when tools were much simpler and today when there are very advanced IDE's available there's no real rationale anymore for it. It's probably kept for historical reason and because nobody really would become that much happier if they could dump more than one class in the same file.

  • How to get class name of a object in run time, from its accessible context.

    Hi,
    I need to get the class name of a java object in run-time, given the AccessibleContext of that object.
    I gone through the AccessibleContext api documentation. but there is not way to get the class name for a java object using its AccessibleContext object.
    Do any one have any idea how to get the class name of an java object, given its accessible object Accessible.
    Thanks
    Timberlake

    816311 wrote:
    Please try to provide a solution for my requirement and avoid evaluating a requirement.
    I am a curious guyit's great to be curious. however, in this situation, the requirement makes no sense in the given context. so, in an effort to be helpful, the people on this forum are asking you the reason behind the requirement. the reason we do this is because we have experience answering questions on this forum and, more often than not, requirements which don't make sense are the result of misunderstandings or confusion on the part of the person making the requirement. if we can figure out why you want to do what you want to do, we may be able to point you in a direction which makes more sense.

  • Why does Flash generate invalid class names for library items?

    I imported a bunch of bitmaps (PNGs).  The library symbol names all included the full file name and its extension.  When I check "export for actionscript" it generates a class name based on the symbol name, and it includes the ".png", even though that's invalid in a class name.
    When I select multiple bitmaps and choose properties and export for actionscript, it has a note saying that classnames will be automatically generated for all items, but it does the same thing... including the ".png" in all the class names.  Is this a problem?  Wouldn't it save time if Flash would automatically strip the file extension from the instance name of bitmap library symbols when generating class names?

    Thank you Nipun.  That is exactly the behavior I would expect, and I can confirm that in the trial of Flash CC it generates valid class names by removing the file extension.  For example, a file named "3ball.test.png" will receive an auto-generated class name of "_3ball_test", so it really does ensure the class name is valid (meeting AS3 class name syntax)
    kglad, what I'm saying is that an image file name like "ball.png" is not a valid "class name", because it contains a period, which is reserved as a package delimiter.  I know the runtime can handle it as a valid "fully-qualified class name", but it's misleading because it's actually creating a class named "png" in package "ball", BUT it's understood that the auto-generated name is not supposed to be specifying any package, hence the misleading specification.  The class would have to be instantiated like this: "import ball.png; var bd:BitmapData = new png();" or "import ball.*; var bd:BitmapData = new png();"  You would also then have to import every single class individually and always use their fully qualified names, because you'd technically have a bunch of classes all named "png" inside a hundred different packages.  It's just not acceptable.
    The other problem that occurred in CS6 is if you loaded a bunch of files whose names started with numbers, then when you select them all, choose properties, and use the batch properties window to export them for actionscript, I noticed that such images would be auto-assigned generic names like "Class4" as it would realize the default auto-generated class name would be invalid.  This problem has been fixed beautifully in Flash CC.
    Nipun, it would actually be very helpful to have a little more control over the class naming conventions.  For example, if I wanted all my BitmapData classes to exist in an "images" folder or package, it would be helpful if in the batch properties window I could specify a package name for all the auto-generated class names.  When importing hundreds of images, it's very tedius to have to edit them individually and I don't always have time to write a JSFL script.  There's room in that window to add a text field for a package name.  Thanks!

  • Why Hashtable class name is like this instead of HashTable

    I have a doubt in Collections, why Hashtable class name is like this, because, Every class name is start with CAPS like, ArrayList, SortedSet, etc. then the second name first letter should be in CAPS.
    Is that any special for that. Can anyone clear my doubt
    Thanks in advance.<!--Session data-->

    DrClap wrote:
    Because that's what they called it twelve years ago when they wrote it. I don't really think that asking about what a couple of people were thinking twelve years ago when they named a class is a good use of anybody's time. In fact I think it's a complete waste of time.may be

  • Why rmiregistry & Server must start in the same directory.

    I have 2 servers & one rmiregisty for both. I want to start this servers separetely from different directories.
    But if I try to start server from the directory other than where I've already started rmiregistry the server doesn't bind.
    Please, can anyone explain why rmiregisty & server must start from the same directory?

    Are you sure the classpath when rmiregistry is started includes jar/directories where rmiregistry can find your server stubs ???
    rmiregistry does not need your implementation, but it needs your stubs to be able to register your server objects ...

Maybe you are looking for

  • GAPs in financial year periods

    i have encountered an error whie maintaining financial year variants. When maintaining the fiscal year variants in Transaction OB29 for variant WK, the following error message appears: "Gaps in financial year periods XY" (FGV 556), even though there

  • Please help me on this!!!(MB_MIGO_BADI)

    Dear all, I am presently working on MB_MIGO_BADI.The requirement is to create additional 'DETAIL' screen for customer data.I have created the screen and provide all the on-screen functionalities. Now, I have following queries: 1)How can I access the

  • Basic question on classification

    Hi, Why is classification used? How does it help beside being categorised differently than the actual folder structure? Please clarify. Thanks and regards, Arun

  • Report two rows for a given group item

    All, I need to generate a report that will list credits on one line and related debits on the next line. Kind of like the following: Account Name_____Amount Cash ................. 1000.00 ...... (this line would reflect positive entries, or deposits)

  • Pointing to central SLD inplace of local SLD

    Hi , I want to change point local SLD to central sld link in  http://hostname:50000/rep/start/index.jsp. Please advise me. Regards, Kirthi S