• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ssuchieh li
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sumit kothalikar
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ssuchieh li
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic