File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes package directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package directory" Watch "package directory" New topic
Author

package directory

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
Hi,

I was going through following question from site
http://www.jchq.net/certkey/0102certkey.htm


Question 9)What happens when you attempt to compile and run these two files in the same directory?
//File P1.java
package MyPackage;
class P1{
void afancymethod(){
System.out.println("What a fancy method");
}
}
//File P2.java
public class P2 extends P1{
afancymethod();
}

Answer 9)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

I have not understood answer. does directory and package both refer same. please advise. thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
P1 is in the MyPackage package, and has package-private access. P2 is in the unnamed package, and cannot see non-public classes in that package.
Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
>>and cannot see non-public classes in that package.

I have not understood clearly. Can you please elaborate.Can it see default scoped ones. please advise
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Mathew Lee wrote:>>and cannot see non-public classes in that package.

I have not understood clearly. Can you please elaborate.Can it see default scoped ones. please advise

The default moderator (package private) means only classes in the same package can access it. Since p2 is in a different package, it cannot.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Jeanne Boyarsky wrote:The default moderator (package private)

You mean modifier instead of moderator, right?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: package directory