Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes package name and class name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package name and class name" Watch "package name and class name" New topic

package name and class name

lyko saha

Joined: Aug 21, 2004
Posts: 5
In Chapter 6: Objects and Classes (Sun Website) By Simon Roberts, Philip Heller, and Michael Ernest
The authors mentioned that:
�It is illegal for a package and a class to have the same name, so there can be no ambiguity.�

However, when I tried to compile the following code, it was successful.
Why is it so? I mean, how can I prove it?

package P1;
class P1 {}

This is saved as in a directory P1 (which is again in a directory specified in my classpath)
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32635

Welcome to JavaRanch! I think that means that you can't have a package and class with the same name at the same level.

For example:
1) package jeanne.Test;
2) package jeanne;
class Test() { ... }

This would be illegal because jeanne.Test would be ambiguous.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Note that the fully qualified name of the class you posted is P1.P1, while the name of the package is simply P1. So, P1.P1 != P1

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: package name and class name
It's not a secret anymore!