File name vs. public class?

Could anyone tell me that why should the file name be the same as the only public class in that file? Is there any reason for this rule? Thanks in advance. I really appreciate it.
David

A java restrication rule and only one class declared public.

Similar Messages

  • How can I assign image file name from Main() class

    I am trying to create library class which will be accessed and used by different applications (with different image files to be assigned). So, what image file to call should be determined by and in the Main class.
    Here is the Main class
    import org.me.lib.MyJNIWindowClass;
    public class Main {
    public Main() {
    public static void main(String[] args) {
    MyJNIWindowClass mw = new MyJNIWindowClass();
    mw.s = "clock.gif";
    And here is the library class
    package org.me.lib;
    public class MyJNIWindowClass {
    public String s;
    ImageIcon image = new ImageIcon("C:/Documents and Settings/Administrator/Desktop/" + s);
    public MyJNIWindowClass() {
    JLabel jl = new JLabel(image);
    JFrame jf = new JFrame();
    jf.add(jl);
    jf.setVisible(true);
    jf.pack();
    I do understand that when I am making reference from main() method to MyJNIWindowClass() s first initialized to null and that is why clock could not be seen but how can I assign image file name from Main() class for library class without creating reference to Main() from MyJNIWindowClass()? As I said, I want this library class being accessed from different applications (means different Main() classes).
    Thank you.

    Your problem is one of timing. Consider this simple example.
    public class Example {
        public String s;
        private String message = "Hello, " + s;
        public String toString() {
            return message;
        public static void main(String[] args) {
            Example ex = new Example();
            ex.s = "world";
            System.out.println(ex.toString());
    }When this code is executed, the following happens in order:
    1. new Example() is executed, causing an object to constructed. In particular:
    2. field s is given value null (since no value is explicitly assigned.
    3. field message is given value "Hello, null"
    4. Back in method main, field s is now given value "world", but that
    doesn't change message.
    5. Finally, "Hello, null" is output.
    The following fixes the above example:
    public class Example {
        private String message;
        public Example(String name) {
            message = "Hello, " + name;
        public String toString() {
            return message;
        public static void main(String[] args) {
            Example ex = new Example("world");
            System.out.println(ex.toString());
    }

  • Why files with no public classes have no naming restrictions ?

    As i know a file with a public class ,uch have the same name as the class name but the files with no public classes have no naming restrictions and then how we can access these classes ?

    but the files with no public classes have no naming restrictions and then how we can access these classes ?You can access a class in a files with no public classes have no naming restrictions by just creating their object.
    Here is the code you can run.
    File: NonPublic.java
    * @(#)NonPublic.java
    * @author Gagan Ichake
    * @version 1.00 2009/1/15
    class NotAsFileName{
         void baz()
              System.out.println("I am in NonPublic class.");
    }File: AccessNonPublic.java
    * @(#)AccessNonPublic.java
    * @author Gagan Ichake
    * @version 1.00 2009/1/15
    public class AccessNonPublic {
            public static void main(String []arg)
              NotAsFileName nafn = new NotAsFileName();
              nafn.baz();
    }Edited by: Ggn on Jan 15, 2009 10:53 PM

  • How to specify relative path for file name in java class

    I have a directory structure like this.
    C:\Aurora\com\optemys\aurora\validation
    I have a class Test.java under the validation folder.
    I have various files under the same folder i.e validation.
    If I want to access the file "test.properties" in Test.java, how can I just specify "test.properties" & not "C:\Aurora\com\optemys\aurora\validation\test.properties".
    I dont want to hardcode any file path into the class. How can I specify the relative file name into the class.
    Thanks In Advance
    Sachin

    switch the name in Test.properties and use
    this.getClass().getResourceAsStream(fileName)

  • Why have the file name MyClass$1.class

    Hi all,
    Please exlain why have class file with $1, $2 (example: MyClass$1.class)
    If i delete it, do my program run ?
    thanks

    Hi all,
    Please exlain why have class file with $1, $2
    (example: MyClass$1.class)
    Those are anonymous inner classes you created in your MyClass code.
    If i delete it, do my program run ?Not well anyway.

  • Why is the name of java file is same as public class?

    hello friends
    why we need to assign same name to .java file as the name of public class in .java file? while its not necessary for the class having no modifier?

    This question has been asked several times. Serch the forum.
    x

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

  • Class name and file name should be same??

    Hi All,
    I am having tweo public classes in a file like this:
    public class A
    // Class body
    public class B extends A
    // Class body
    public static void main(String args[])
    // Function body
    when I compile its giving the following error:
    B.java:1: class A is public, should be declared in a file named A.java
    Why it is so.whats wrong in this.
    Thanks in advance,
    Jana.

    In C you have to write a "make-file" for your project (or it's written for you).
    In java there are no "projects". If the JVM needs a class it is looking for a file with this name in the classpath.

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

  • Jar file name Required for IApplicationPropertiesService class file

    Hi
       I am using EP6 SP15, Please let me know the jar file name for IApplicationPropertiesService.class
    Regards
    Ganesan S

    Hi,
    you should have it in your NWDS plugins directory. I have it here:
    <i>\Program Files\SAP\JDT\eclipse\plugins\com.sap.km.rfwizard\lib\bc.rf.global.service.appproperties_api.jar</i>
    Romano
    PS: you can try to find it yourself - not so hard: just search in the filesystem for XXXXXX.class including searching archives...

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

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

Maybe you are looking for

  • How do I get apps I've already bought on my phone to show on my new iPad?

    I just purchased an iPad mini.  I restored it from my iPhone, rather than setting it up as a new iPad, thinking that I would then be able to see all my apps,  However, none of the apps I have on my iPhone are available on the iPad.  I went to the app

  • CL_GUI_FRONTEND_SERVICES- FILE_DELETE   --  use???

    How to delete a file from the presentation server (desktop) using the following method?? explain in detail with sample code. CL_GUI_FRONTEND_SERVICES->FILE_DELETE Regards, Kalai

  • Table name for Customer Account Group and created by Data

    Dear Gurus, Kindly le t me know the table name having a list of Customer a/c groups and created by data. if there is no such table thn pls let me know the alternatives for fetching the same data. Wishes Abhishek

  • Hi8 video

    How can I best transfer my old Hi8 analog tapes to the HD on my iMac... or to a external HD. My "new" (2006) miniDV camera does not have "pass-through" capability. Purchase a converter? What are good choices?

  • Logic with Reason or Live?

    Hello all and Happy Holidays. I'm looking to add some more flexibility to Logic. Right now I am trying to decide between adding "Reason" or "Live" to my software arsenal but I'm unsure which is the better choice. For the short term it has to be one o