Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java classes are always defined inside a single source code file ?!

Reply
Thread Tools

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

 
 
Razvan
Guest
Posts: n/a
 
      07-15-2004
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
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      07-15-2004
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.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-15-2004
On 15 Jul 2004 06:50:49 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to put a single quote in a JS var defined by single quotes gimme_this_gimme_that@yahoo.com Javascript 2 07-07-2008 03:00 AM
Backticks with single quote inside single quotes john.d.russell@gmail.com Perl Misc 10 04-17-2006 01:31 AM
OOP casting classes (using one single variable for similar types of classes) roberts.noah@gmail.com C++ 8 01-07-2006 04:13 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
Trying to create a CSS box that is always is always the width of an image placed inside it (and no wider) Deryck HTML 4 06-22-2004 08:25 PM



Advertisments