jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question:Kathy's book P.11 (there is no way to declare multiple classes in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question:Kathy Watch "Question:Kathy New topic
Author

Question:Kathy's book P.11 (there is no way to declare multiple classes in a file

ssuchieh li
Greenhorn

Joined: May 01, 2010
Posts: 6
hi, guys...

I am really confusing about the P.11
Hope anyone who knows better could give me an example:

the line says... there's no way to declare multiple classes in a file and have them in different packages or use different imports.

Thanks
Jessy
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

It is not like that
you CANNOT write multiple PUBLIC classes in a single source file
you CAN write multiple non public classes in a single source file
Hope this helps


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91



Prasad says:
It is not like that
you CANNOT write multiple PUBLIC classes in a single source file
you CAN write multiple non public classes in a single source file
Hope this helps

If a class is declared public no other class can be public in that source file.


Thanks & Regards
Sumit Kothalikar
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

ssuchieh li wrote:the line says... there's no way to declare multiple classes in a file and have them in different packages or use different imports.

That means when you define classes (one public at most and any non-public) they belong to the same package defined in the top of the file if any (or default package if not) and the import statement(s) defined applied to all the classes within the file.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
ssuchieh li
Greenhorn

Joined: May 01, 2010
Posts: 6
Thank you guys...
I know the point you guys point out.
I should make my question more clear...

for example:
I have a AA.java file and it is defined:

package A;
import B

public class AA{

}

class C{}
class D{}

so according to "there's no way to declare multiple classes in a file and have them in different packages"
does it mean
I can not have class C{} and class D {} in different package....? or you could have example for me to understand "and have them in different packages"...


Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

ssuchieh li wrote:so according to "there's no way to declare multiple classes in a file and have them in different packages"
does it mean I can not have class C{} and class D {} in different package....?

Yes, according to your example as they are defined in the same source file and belong to the package A. And Welcome to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question:Kathy's book P.11 (there is no way to declare multiple classes in a file
 
Similar Threads
a problem of multiple inheritance
Access to Constrained Resources
How Java handles class files?
How To Placed variables in Abstract classes
How can multiple inheritance solve the problem of Deadly Diamond of Death