File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Package visibility Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package visibility" Watch "Package visibility" New topic

Package visibility

Willie Toma
Ranch Hand

Joined: May 11, 2001
Posts: 78
I don't understand this answer? Here is the Q&A.
What happens when you attempt to compile and run these two files in the same directory?
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
public class P2 extends P1{
1) Both compile and P2 outputs "What a fancy method" when run
2) Neither will compile
3) Both compile but P2 has an error at run time
4) P1 compiles cleanly but P2 has an error at compile time
4) P1 compiles cleanly but P2 has an error at compile time
Even though P2 is in the same directory as P1, because P1 was declared with the package statement it is not visible from P2
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Actaully, it's quite simple.
To use a package, you need to import it. If you add import MyPackage in, then it's fine.
Remember, package != directory.
I agree. Here's the link:
subject: Package visibility