Why the name of a file that contains java code should be same as the name of the class that is conta

Discussion in 'Java' started by corejavagroups, Nov 20, 2005.

  1. Hello Friends!
    I am new to java area.
    Why should we give the name of a file as same as the name of the class
    that is contained in it?
    Can you please explain it?
    Please don't send me the links....i know thousands of links
    available...but not in a proper way...that's why i am asking for a
    straight answer......if you don't know please Ignore it
    Once again i am telling i am new to java platform....
    OK Thank you...
    Bye
     
    corejavagroups, Nov 20, 2005
    #1
    1. Advertising

  2. corejavagroups

    Guest

    Hi ...ehm... "corejavagroups".
    > I am new to java area.

    Your nick indicates this...
    > Why should we give the name of a file as same as the name of the class
    > that is contained in it?

    How do you invoke a java programm that consist of only one class ? You
    give the JRE the name of the class, and the JRE looks for a file called
    "ClassName.class" and loads the class. If it is not there, you get an
    error. Have you thought of the confusion that could arise when this is
    not required? How would java find a class with a specific name if it
    didn´t know in what file it is located? And if you were not talking
    about *class files, but about source code: How would you look for the
    source of the public class "HelloWorld" when you didn´t have any clue
    about the name of the source code file?
    > Can you please explain it?
    > Please don't send me the links....i know thousands of links available...
    > but not in a proper way...

    And YOU know what is proper...
    > that's why i am asking for a straight answer......if you don't know please Ignore it
    > Once again i am telling i am new to java platform....
    > OK Thank you...

    You´re welcome. But now stop philosophising and write some code! There
    are MANY MORE questions waiting for you...
    Good luck
    Piet
     
    , Nov 20, 2005
    #2
    1. Advertising

  3. corejavagroups

    Guest

    hi

    IT IS NOT MANDATORY (unless the class is public).
    If you write a class

    class SampleClass
    {
    ....
    }
    you can name it MyClass.java because the class is not public.

    Only for public classes it is MANDATORY to have the same name for the
    java file.

    as for the reasoning im not 100% sure but its better to have public
    class A defined in A.java because your class is acessible to public
    (i.e across your package) so if required they should locate it easily.

    but when your class is not public then it is visible only within your
    package so you know which file has which class.

    that is how you can also write 3-4 classes in the same file provided
    they are not (or at most 1 is) public.

    when you run java you dont give it the filename the argument is the
    ClassName (which contains main method)

    HTH
    cheers
    amey
     
    , Nov 20, 2005
    #3
  4. corejavagroups

    Roedy Green Guest

    On 19 Nov 2005 22:00:03 -0800, "corejavagroups"
    <> wrote, quoted or indirectly quoted
    someone who said :

    >Why should we give the name of a file as same as the name of the class
    >that is contained in it?


    It makes like easy for the compiler to find the source code for a
    class it needs. When compiling class A you make reference to a method
    of class B, Java has to find the corresponding class B file or if that
    is missing, the corresponding source so that it knows how to generate
    the code in class A. Having a consistent naming system makes that
    faster.

    It also makes it easy for programmers to find the right code given a
    class name from JavaDoc or other source.

    I am so glad Java enforces this rule. Other languages create needless
    chaos by not enforcing something similar.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 20, 2005
    #4
  5. corejavagroups

    Roedy Green Guest

    On 20 Nov 2005 02:47:10 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >Only for public classes it is MANDATORY to have the same name for the
    >java file.


    You are teaching bad habits. It is not mandatory to brush your teeth
    each morning either.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 20, 2005
    #5
  6. corejavagroups

    Mickey Segal Guest

    "Roedy Green" <> wrote in
    message news:...
    > I am so glad Java enforces this rule. Other languages create needless
    > chaos by not enforcing something similar.


    The rule is not really enforced in a stringent way because you can have 50
    classes in one JAVA file; all you need to do is name the file the same as
    one of the classes. Some will argue that one should store all of the
    classes in 50 separate files. While this makes sense in many programming
    situations, it is burdensome in others, particularly when multiple classes
    are in one inheritance tree and when few programmers are involved with
    maintaining that set of classes.
     
    Mickey Segal, Nov 20, 2005
    #6
  7. corejavagroups

    Roedy Green Guest

    On Sun, 20 Nov 2005 11:43:06 -0500, "Mickey Segal"
    <> wrote, quoted or indirectly quoted someone
    who said :

    >The rule is not really enforced in a stringent way because you can have 50
    >classes in one JAVA file; all you need to do is name the file the same as
    >one of the classes.

    It has the name of the PUBLIC class. You can't put two public
    classes in the same source file. Ideally, every top level class
    should have get its own source file, public or not, with a matching
    name.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 21, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    693
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,266
    Smokey Grindel
    Dec 2, 2006
  3. Joseph Turian
    Replies:
    5
    Views:
    631
  4. Daniel Jovanovski

    Conta occorrenze in Hash

    Daniel Jovanovski, Nov 5, 2010, in forum: Ruby
    Replies:
    2
    Views:
    216
    Jesús Gabriel y Galán
    Nov 5, 2010
  5. Pavils Jurjans
    Replies:
    3
    Views:
    239
    Pavils Jurjans
    Mar 18, 2005
Loading...

Share This Page