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

R

Razvan

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
 
M

Michael Borgwardt

Razvan said:
"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.
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top