Why not two classes...

D

Daniel Pitts

Why cann't we have two public classes in one file....

Because the convension is that a public class must be defined in a
file with that same name.
A file cannot have two names, and therefore must contain at most one
public class.
Note that you can have as many non-public classes are you want, and
you also don't have to have any public classes.
 
P

Piotr Kobzda

Shraddha said:
Why cann't we have two public classes in one file....

We can have as many public classes as we want in one compilation unit --
one-or-more top-level, and zero-or-more nested classes.

Limit of one public top-level class per compilation unit is optional
restriction imposed only when packages are stored in a local file
system. According to JLS (7.7.2), this restriction must not be imposed
when packages are stored in a database.


piotr
 
P

Piotr Kobzda

Shraddha said:
Why cann't we have two public classes in one file....

We can have as many public classes as we want in one compilation unit --
one-or-more top-level, and zero-or-more nested classes.

Limit of one public top-level class per compilation unit is optional
restriction imposed only when packages are stored in a local file
system. According to JLS (7.2.2), this restriction must not be imposed
when packages are stored in a database.


piotr
 
C

Chris Smith

Shraddha said:
Why cann't we have two public classes in one file....

You can't do it because of a completely arbitrary choice made by the
implementor of your compiler. The Java Language Specification suggests
it, but it's not really even a requirement of the Java language.

Presumably, your compiler implementor made this choice because he or she
felt that it is a good idea for you to organize code that way; and that
it would be helpful for the compiler to enforce this.
 

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,159
Messages
2,570,886
Members
47,419
Latest member
ArturoBres

Latest Threads

Top