Catfish
Catfish

Reputation: 709

Confusion regarding Java Packages and Windows Directory?

When preparing for the SCJP exam, we were going through the following code:

package certificaton;    
public class OtherClass    
{    
 public void testIt()    
  {    
     System.out.println("otherclass");    
  }    
} 

And this:

package somethingElse;    
import certification.OtherClass;    
public class AccessClass    
{    
   public static void main( String args[])    
   {    
       OtherClass o= new OtherClass();    
       o.testIt();    
   }    
}

I placed both the above files in the following directory: C:\scjp\temp8 ; and the strange thing is that, the .java files are compiling and results in two .class files being created in the same directory. The thing I want to ask, is that, the difference between packages and directory. Isn't it true that the class files could be created in a directory other than the one stated in the package declaration? And the package declaration is something 'virtual', and disregards the windows directory structure. In addition, isn't it also true that, by executing the following command:

javac -d . OtherClass.java 

The directories are created conforming to the package declaration, which isn't always mandatory?

Upvotes: 0

Views: 152

Answers (1)

duffymo
duffymo

Reputation: 308753

The directories are created conforming to the package declaration, which isn't always mandatory?

No, the package and directory structures must match. It's mandatory, not optional.

Upvotes: 1

Related Questions