File name different to the class name

Discussion in 'Java' started by qazmlp, Aug 19, 2003.

  1. qazmlp

    qazmlp Guest

    GUI@sn65(*)[86]: more different_file_name.java
    import java.util.* ;

    class println_null
    {
    public static void main(String arg[] )
    {
    Vector vec = null ;
    System.out.println("I'm printing null" +vec ) ;

    }

    }
    GUI@sn65(*)[87]: javac different_file_name.java
    GUI@sn65(*)[88]: java println_null
    I'm printing nullnull


    As you can see above, the file name and the class name are different and it works.
    But, I am getting compilation error, when I change
    class println_null
    to
    public class println_null

    Why?
     
    qazmlp, Aug 19, 2003
    #1
    1. Advertising

  2. If your class isnt public the file name is allowed to be different - if your
    class is public it has to have the same name as the file!


    *hth*


    "qazmlp" <> schrieb im Newsbeitrag
    news:...
    > GUI@sn65(*)[86]: more different_file_name.java
    > import java.util.* ;
    >
    > class println_null
    > {
    > public static void main(String arg[] )
    > {
    > Vector vec = null ;
    > System.out.println("I'm printing null" +vec ) ;
    >
    > }
    >
    > }
    > GUI@sn65(*)[87]: javac different_file_name.java
    > GUI@sn65(*)[88]: java println_null
    > I'm printing nullnull
    >
    >
    > As you can see above, the file name and the class name are different and

    it works.
    > But, I am getting compilation error, when I change
    > class println_null
    > to
    > public class println_null
    >
    > Why?
     
    Andreas Schachl, Aug 19, 2003
    #2
    1. Advertising

  3. qazmlp

    Stefan Siegl Guest

    qazmlp wrote:

    > GUI@sn65(*)[86]: more different_file_name.java
    > import java.util.* ;
    >
    > class println_null
    > {
    > public static void main(String arg[] )
    > {
    > Vector vec = null ;
    > System.out.println("I'm printing null" +vec ) ;
    >
    > }
    >
    > }
    > GUI@sn65(*)[87]: javac different_file_name.java
    > GUI@sn65(*)[88]: java println_null
    > I'm printing nullnull
    >
    >
    > As you can see above, the file name and the class name are different and it works.
    > But, I am getting compilation error, when I change
    > class println_null
    > to
    > public class println_null
    >
    > Why?


    btw I would suggest using the List interface instead of the Vector class
    (if you do not do this already in your other classes)
     
    Stefan Siegl, Aug 19, 2003
    #3
  4. qazmlp

    rkm Guest

    Dario wrote:
    > qazmlp wrote:
    >
    >> Why?

    >
    >
    > Only public classes must be declared in files with proper names.
    >
    > - Dario
    >


    Not all IDEs enforce this, so if he/she was using an IDE,
    then it's possible to get around it. Codewarrior, in
    particular, allows you to set a switch to tell it to not
    enforce this oddness.

    rkm
     
    rkm, Aug 20, 2003
    #4
  5. qazmlp

    qazmlp Guest

    Dario <> wrote in message news:<bhslfg$oh2$>...
    > qazmlp wrote:
    >
    > > Why?

    >
    > Only public classes must be declared in files with proper names.
    >


    Apologies if my question was not clear.

    All of you have just repeated what I also observed.But, I wanted to
    know why is it so?
    I wanted to know why that constraint(file name having the same name as
    the class name) exists when I make it as public and not when it has
    the default access specifier.
     
    qazmlp, Aug 20, 2003
    #5
  6. qazmlp

    Dario Guest


    >> Only public classes must be declared in files with proper names.
    >>

    > Apologies if my question was not clear.
    >
    > All of you have just repeated what I also observed.But, I wanted to
    > know why is it so?
    > I wanted to know why that constraint(file name having the same name as
    > the class name) exists when I make it as public and not when it has
    > the default access specifier.


    You can have one single file F.java with many (e.g. 3: A, B, C)
    classes with default access specifier but (at max) only one public
    class (e.g. F).

    File F.java:
    /////////////////////////////////////////////////////////////////////
    class A{public static void main(String[]a){System.out.println("A");}}
    class B{public static void main(String[]a){System.out.println("B");}}
    class C{public static void main(String[]a){System.out.println("C");}}
    public
    class F{public static void main(String[]a){System.out.println("F");}}
    /////////////////////////////////////////////////////////////////////

    and you can invoke them in one of the following 4 ways:

    prompt> java A
    A
    prompt> java B
    B
    prompt> java C
    C
    prompt> java F
    F

    but you cannot add another public class G to file F.java:
    /////////////////////////////////////////////////////////////////////
    class A{public static void main(String[]a){System.out.println("A");}}
    class B{public static void main(String[]a){System.out.println("B");}}
    class C{public static void main(String[]a){System.out.println("C");}}
    public
    class F{public static void main(String[]a){System.out.println("F");}}
    public
    class G{public static void main(String[]a){System.out.println("G");}}
    /////////////////////////////////////////////////////////////////////

    Why this?

    Because a compiler acting on compilation units residing
    on a filesystem may choose to enforce the restriction
    that there must be at most one public class per compilation unit.

    This optional restriction is allowed by Java Language Specification,
    <http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#73491>.

    As pointed out by the Java Language Specification:

    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.

    This restriction is optional, as pointed out rkm in message
    <http://makeashorterlink.com/?C31E555A5>.

    This restriction is forbidden by Java Language Specification
    when packages are stored in databases (instead of filesystem):
    <http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#20150>.

    I hope this answer clarify your doubts.

    - Dario
     
    Dario, Aug 20, 2003
    #6
  7. (qazmlp) writes:
    > All of you have just repeated what I also observed.But, I wanted to
    > know why is it so?


    Because the language designers decided so. There is nothing more behind
    it. They thought it was a good idea to require to have the same name
    for a public class and the file. It is an enforcement of a common
    coding convention you e.g. find among C++ programmers.

    When you decide that the name of a public class has to match the file
    name, you have to live with some consequences:

    - At most only one public class per file for obvious reasons.

    - If you have a public class in the file, it has the file's name. So if
    you allow other classes in the file, too (again something which is
    allowed in C++), these ones can't have the same name as the file
    (that one is taken by the public class). So you must allow these
    classes to have different names. It just't can't work otherwise.

    And that's all. There is no mystery behind it or some deep technology
    breakthrough. It is just a design decission and some logical
    consequences of that decission.

    /Thomas
     
    Thomas Weidenfeller, Aug 20, 2003
    #7
  8. Jacob <> writes:
    > The original question is still not answered:
    > Why is it not so for a non-public class?


    I answered that: If you enforce it for public classes, you can't
    enforce if for non-public classes:

    >> - If you have a public class in the file, it has the file's name. So if
    >> you allow other classes in the file, too (again something which is
    >> allowed in C++), these ones can't have the same name as the file
    >> (that one is taken by the public class). So you must allow these
    >> classes to have different names. It just't can't work otherwise.


    /Thomas
     
    Thomas Weidenfeller, Aug 20, 2003
    #8
  9. qazmlp

    Jacob Guest

    Thomas Weidenfeller wrote:
    > Jacob <> writes:
    >
    >>The original question is still not answered:
    >>Why is it not so for a non-public class?

    >
    >
    > I answered that: If you enforce it for public classes, you can't
    > enforce if for non-public classes:


    Sorry. You did. Thanks!
     
    Jacob, Aug 20, 2003
    #9
  10. qazmlp

    qazmlp Guest

    Dario <> wrote in message news:<bhv9vr$khd$>...
    > >> Only public classes must be declared in files with proper names.
    > >>

    > > Apologies if my question was not clear.
    > >
    > > All of you have just repeated what I also observed.But, I wanted to
    > > know why is it so?
    > > I wanted to know why that constraint(file name having the same name as
    > > the class name) exists when I make it as public and not when it has
    > > the default access specifier.

    >
    > You can have one single file F.java with many (e.g. 3: A, B, C)
    > classes with default access specifier but (at max) only one public
    > class (e.g. F).
    >
    > File F.java:
    > /////////////////////////////////////////////////////////////////////
    > class A{public static void main(String[]a){System.out.println("A");}}
    > class B{public static void main(String[]a){System.out.println("B");}}
    > class C{public static void main(String[]a){System.out.println("C");}}
    > public
    > class F{public static void main(String[]a){System.out.println("F");}}
    > /////////////////////////////////////////////////////////////////////
    >
    > and you can invoke them in one of the following 4 ways:
    >
    > prompt> java A
    > A
    > prompt> java B
    > B
    > prompt> java C
    > C
    > prompt> java F
    > F
    >
    > but you cannot add another public class G to file F.java:
    > /////////////////////////////////////////////////////////////////////
    > class A{public static void main(String[]a){System.out.println("A");}}
    > class B{public static void main(String[]a){System.out.println("B");}}
    > class C{public static void main(String[]a){System.out.println("C");}}
    > public
    > class F{public static void main(String[]a){System.out.println("F");}}
    > public
    > class G{public static void main(String[]a){System.out.println("G");}}
    > /////////////////////////////////////////////////////////////////////
    >
    > Why this?
    >
    > Because a compiler acting on compilation units residing
    > on a filesystem may choose to enforce the restriction
    > that there must be at most one public class per compilation unit.
    >
    > This optional restriction is allowed by Java Language Specification,
    > <http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#73491>.
    >
    > As pointed out by the Java Language Specification:
    >
    > 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.
    >
    > This restriction is optional, as pointed out rkm in message
    > <http://makeashorterlink.com/?C31E555A5>.
    >
    > This restriction is forbidden by Java Language Specification
    > when packages are stored in databases (instead of filesystem):
    > <http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#20150>.
    > I hope this answer clarify your doubts.


    Yes, it did.
    Thanks a lot for your detailed and clear explanation.
     
    qazmlp, Aug 20, 2003
    #10
  11. qazmlp

    Roedy Green Guest

    On Wed, 20 Aug 2003 10:21:20 +0000 (UTC), lid
    (Thomas Weidenfeller) wrote or quoted :

    >Because the language designers decided so. There is nothing more behind
    >it. They thought it was a good idea to require to have the same name
    >for a public class and the file.


    Naming the file the same as the class has a practical purpose, for
    allowing the Java compiler to rapidly find the source for a class if
    it can't find the the *.class file.

    For example when the compiler is compiling class A and needs to
    compile call to a method from class B, it either has to find the class
    file for B or the source file for B to know how to do it.
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 20, 2003
    #11
    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. Replies:
    0
    Views:
    464
  2. Spacey Spade
    Replies:
    4
    Views:
    643
    Remon van Vliet
    Jun 15, 2005
  3. E11
    Replies:
    1
    Views:
    4,902
    Thomas Weidenfeller
    Oct 12, 2005
  4. Fabrizio Pollastri
    Replies:
    2
    Views:
    352
  5. udoline
    Replies:
    2
    Views:
    157
    udoline
    Sep 24, 2003
Loading...

Share This Page