Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

two public class in a single file

Thread view: 
mantu - 11 May 2006 06:50 GMT
i have two public class in a single file.it is giving compilation error
so can any one tell what is the resion behind this.
i know it will give but i dont know what is the think on this.
Stefan Schulz - 11 May 2006 08:28 GMT
AFAIK it is simply a convention - but i believe it is a good one, since
it actually makes you seperate different aspects of your program
(seperate public classes) into seperate files
Axel Hallez - 11 May 2006 08:50 GMT
> i have two public class in a single file.it is giving compilation error
> so can any one tell what is the resion behind this.
> i know it will give but i dont know what is the think on this.

The java compiler requires a public class to be in a file with the same
name (i.e. MyClass should be in MyClass.java which will be compiled to
MyClass.class). This is to enable the classloader to find the class when
it is needed.
As a result you can have only one public class in a file.

Axel Hallez
Simon - 11 May 2006 12:09 GMT
>> i have two public class in a single file.it is giving compilation error
>> so can any one tell what is the resion behind this.
[quoted text clipped - 5 lines]
> it is needed.
> As a result you can have only one public class in a file.

I think the OP is asking why you can have only one class per *source* file.
(Compiling a single source file with several public classes could still create
several .class files such the classloader could find all of them.)

Here is another reason: Assume you compile A.java which depends on class B.
Having only one public class per source file, the compiler can find the source
B.java, compile that first, and then compile A.java. (Try javac -sourcepath.)

Cheers,
Simon
opalpa@gmail.com opalinski from opalpaweb - 11 May 2006 13:53 GMT
> The java compiler requires a public class to be in a file with the same
> name

Filenames are ASCII, class names are Unicode.  There is a mapping
between classnames and how to find them.  Most of the time though
you're right: class names and file names match.

Cheers
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Oliver Wong - 11 May 2006 18:15 GMT
<opalpa@gmail.com> wrote in message
news:1147352034.811134.29300@u72g2000cwu.googlegroups.com...
>> The java compiler requires a public class to be in a file with the same
>> name
>
> Filenames are ASCII, class names are Unicode.  There is a mapping
> between classnames and how to find them.  Most of the time though
> you're right: class names and file names match.

   Depending on the OS, why wouldn't filenames "be unicode"[*] as well?

   - Oliver

[*] the phrase is in quotes because comparing ASCII and Unicode doesn't make
sense, as ASCII both an encoding system and a character set, while Unicode
is a character set without an encoding system.
opalpa@gmail.com opalinski from opalpaweb - 13 May 2006 17:39 GMT
> Depending on the OS, why wouldn't filenames "be unicode"[*] as well?

Yes, in some environments.  My statements were not careful enough.

> [*] the phrase is in quotes because comparing ASCII and Unicode doesn't make
> sense, as ASCII both an encoding system and a character set, while Unicode
> is a character set without an encoding system.

After spending some time reviewing defitiontion of unicode I see that
many, including one from unicode.org
(http://www.unicode.org/faq/basic_q.html#a) talk about unicode being an
encoding as well as character set.

Unicode.org technical introductions tarts with:
"The Unicode Standard is the universal character encoding standard used
for representation of text for computer processing."

Perhaps I am completely misunderstanding your point.  There is plenty
of content on the internet where ASCII and Unicode are compared.  Even
if that comparison is not between two things of the same type, isn't it
fairly clear that when comparing a filename's ASCII bytes equence to a
classname's Unicode byte sequence one cannot perform a byte by byte
match?

Regards,
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Oliver Wong - 13 May 2006 18:36 GMT
<opalpa@gmail.com> wrote in message
news:1147538377.052199.15000@d71g2000cwd.googlegroups.com...
>> [*] the phrase is in quotes because comparing ASCII and Unicode doesn't
>> make
[quoted text clipped - 6 lines]
> (http://www.unicode.org/faq/basic_q.html#a) talk about unicode being an
> encoding as well as character set.

   Hmm, maybe my terminology is a bit loose. I meant that ASCII encodes
characters directly to byte sequences, whereas Unicode is a mapping from
characters to natural numbers (of arbitrary size; e.g. not merely 0 to
2^32), and then you need a seperate encoding, like UTF-8, to map from those
natural numbers to byte-sequences of finite size.

> Unicode.org technical introductions tarts with:
> "The Unicode Standard is the universal character encoding standard used
[quoted text clipped - 6 lines]
> classname's Unicode byte sequence one cannot perform a byte by byte
> match?

   A lot of people (subconciously?) think Unicode maps directly from
characters to byte sequences; it's a common misconception, so it wouldn't
surprise me that there would be a large amount of content on the Internet
which makes this mistake, or gloss over it. AFAIK, there's no such thing as
a "Unicode byte sequence". You could talk about comparing ASCII byte
sequences to UTF-8 byte sequences, but not to "Unicode byte sequences".

    - Oliver
Roedy Green - 14 May 2006 03:57 GMT
>    A lot of people (subconciously?) think Unicode maps directly from
>characters to byte sequences

if you just use 16 bit Unicode, it does.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 15 May 2006 17:40 GMT
>>    A lot of people (subconciously?) think Unicode maps directly from
>>characters to byte sequences
>
> if you just use 16 bit Unicode, it does.

   Only in UTF-16, AFAIK. In UTF-8 (which is one of the few encodings which
is backwards compatible with ASCII when using only characters from the ASCII
character set, and so one of the most commonly used encodings), the unicode
character \u00A9 (the copyright symbol), for example, maps onto the byte
sequence C2A9, and not 00A9.

   - Oliver
Roedy Green - 16 May 2006 20:18 GMT
>    A lot of people (subconciously?) think Unicode maps directly from
>>>characters to byte sequences
>>
>> if you just use 16 bit Unicode, it does.

Let me restate that in a more lawyerly way.

If you use only the codepoints 0..0xffff and you use UTF-16BE
encoding, you can think of Unicode as mapping directly to byte
sequences.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

opalpa@gmail.com opalinski from opalpaweb - 11 May 2006 13:51 GMT
One public class per file is an implementation detail of most java
compilers.

http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.2

Specifically, an alternative is proposed:

"
7.2.2 Storing Packages in a Database
A host system may store packages and their compilation units and
subpackages in a database.

Such a database must not impose the optional restrictions (§7.6) on
compilation units in file-based implementations. For example, a system
that uses a database to store packages may not enforce a maximum of one
public class or interface per compilation unit.
Systems that use a database must, however, provide an option to convert
a program to a form that obeys the restrictions, for purposes of export
to file-based implementations.
"

Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
P Shanks - 02 Jun 2006 01:53 GMT
> i have two public class in a single file.it is giving compilation error
> so can any one tell what is the resion behind this.
> i know it will give but i dont know what is the think on this.

You may have only one public class defined in a class file. You may have
multiple package-private classes (default visibility) in a single file.
All other multiple class-per-file constructs involve inner classes.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.