two public class in a single file

M

mantu

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.
 
S

Stefan Schulz

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
 
A

Axel Hallez

mantu said:
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
 
S

Simon

Axel said:
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.

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
 
O

opalpa

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
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
O

opalpa

The java compiler requires a public class to be in a file with the same

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
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
O

Oliver Wong

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.
 
O

opalpa

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
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
O

Oliver Wong

[*] 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.

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
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?

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
 
O

Oliver Wong

Roedy Green said:
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
 
R

Roedy Green

A lot of people (subconciously?) think Unicode maps directly from

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.
 
P

P Shanks

mantu said:
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.
 

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

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top