This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default package access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default package access" Watch "Default package access" New topic
Author

Default package access

Binkie Hayes
Greenhorn

Joined: Mar 14, 2001
Posts: 20
Hey guys
I've got a bit of a dumb question that's got me all confused!
I've got 2 directories, Folder1 and Folder2 on my C drive, and I've set my classpath to point to Folder1.
In Folder1 I've got this class:
class MyDefaultClass
{
static{System.out.println("Howdy, y'all!");
}
In Folder2 I've got this class:
class Test
{
public static void main(String []args)
{
MyDefaultClass x = new MyDefaultClass();
}
}
Now, I was thinking that Test wouldn't compile because MyDefaultClass is in the default package and the 2 classes are in different directories, but it compiled no problem. I'm presuming it has something to do with the classpath but i dont know what!! any ideas please??! am v confused!
thanks y'all!
binkie
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Binkie,
Even though you put the classes in different directories, they are still in the same package, the default package, since
you did not specify package for your classes.
So, it will compile.
[This message has been edited by Nain Hwu (edited December 10, 2001).]
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Binkie,
If you did something similar to the following:

Then you are specifically telling Java to look for user class files in 'folder1'. In other words, you're telling Java to use 'folder1' as the default package. And, as you didn't include a package statement in either file; they'll both use the current default package.
If you try to compile without setting a classpath, Test.java will produce an error.
Hope that helps.


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Binkie Hayes
Greenhorn

Joined: Mar 14, 2001
Posts: 20
Thanks guys!
 
 
subject: Default package access
 
Similar Threads
Package does not exist
Bug:GhostsFoundInClasspath
A problem with package visiblitly
HELP! default package access
packages and folders