Java classes are always defined inside a single source code file ?!

Discussion in 'Java' started by Razvan, Jul 15, 2004.

  1. Razvan

    Razvan Guest

    Hi !





    "Java classes are always defined inside a single source code file."



    I have found the above afirmation in the book "Java 2 programmer Exam
    Cram 2 (310-035)". However I am able to do something like this in a
    single file:


    public class CTest
    {
    public static void main( String[] args ) throws Exception
    {
    System.out.println("This is CTest !");
    }
    }


    class Mebo {
    public static void main( String[] args )
    {
    System.out.println("This is MEBO !");
    }

    }


    Pay attention: the class Mebo is not public (it has package access).
    However, after compilation I have 2 classes:

    CTest.class
    Mebo.class


    and there is not problem running any of them.

    Question: Is this the normal behaviour or it is just my
    implementation that allows this ?
    Since I am using Sun's compiler and JVM I doubt that they do not obey
    their own standard.




    Regards,
    Razvan
    Razvan, Jul 15, 2004
    #1
    1. Advertising

  2. Re: Java classes are always defined inside a single source code file?!

    Razvan wrote:
    > "Java classes are always defined inside a single source code file."
    >
    > I have found the above afirmation in the book "Java 2 programmer Exam
    > Cram 2 (310-035)". However I am able to do something like this in a
    > single file:

    []
    > Pay attention: the class Mebo is not public (it has package access).
    > However, after compilation I have 2 classes:
    >
    > CTest.class
    > Mebo.class
    >
    >
    > and there is not problem running any of them.
    >
    > Question: Is this the normal behaviour or it is just my
    > implementation that allows this ?


    This is normal. And where do you see a contradiction? Nowhere does it say that
    you can't have more than one class per file, only that you can't spread the
    definition of a class over more than one file - the statement was probably meant
    to specifically distinguish Java fom C++ with its separate header files.

    It is, however, true that a *public* class must always be defined in a file
    of the same name, effectively disallowing more than one public class per file.
    Michael Borgwardt, Jul 15, 2004
    #2
    1. Advertising

  3. Razvan

    Roedy Green Guest

    On 15 Jul 2004 06:50:49 -0700, (Razvan) wrote or
    quoted :

    > Question: Is this the normal behaviour or it is just my
    >implementation that allows this ?
    > Since I am using Sun's compiler and JVM I doubt that they do not obey
    >their own standard.


    A class has to fit in one source file. You must have no more than one
    public class per source file. But other than that, there are
    circumstances when you have more than one class per source file, most
    commonly inner classes.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 15, 2004
    #3
    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. Deryck
    Replies:
    4
    Views:
    517
    derek giroulle
    Jun 22, 2004
  2. Oodini
    Replies:
    1
    Views:
    1,767
    Keith Thompson
    Sep 27, 2005
  3. Replies:
    8
    Views:
    1,355
    Victor Bazarov
    Jan 7, 2006
  4. Replies:
    10
    Views:
    1,202
    Anno Siegel
    Apr 17, 2006
  5. Replies:
    2
    Views:
    125
    slebetman
    Jul 7, 2008
Loading...

Share This Page