File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a class in default invoked by a class in another package 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 "a class in default invoked by a class in another package" Watch "a class in default invoked by a class in another package" New topic
Author

a class in default invoked by a class in another package

Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
I have class A and class B below
// A.java is in c:
public class A {}
// B.java is in c:\mypackage
package mypackage;
public class B {
public static void main(String[] args) {
A a = new A();
}
}
compile from
c:\mypackage\javac B.java
why compile error?
Thanks in advance.
John Pritchard
Ranch Hand

Joined: Nov 15, 2002
Posts: 49
Surasak -
I just started the exam prep a few weeks ago ad have found trying to answer questions of others a really good way to study.
I actually generated a few more questions in looking at yours. In your example, Class A has no access modifier therefore it is "default" e.g. classes of the same package can access it.
Class B is in the "mypackage" package and therefore in a diffeent package than Class A, hence unable to access Class A (thus the compile errors).
Interesting for me though is that I compiled A.java in the c:\mypackage directory so that A.class was in the same directory as B.java. You still get a compile error.
Thus (and please correct me veteran ranch hands) we either need to put Class A in the mypackage package or remove the package declaration from Class B and place A and B in the same directory.


John Pritchard<br />If a JTree falls in the woods, is it Observable
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
Thanks for your answer.
However, is it impossible to import classses in nonpackage for classes in another packages?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The key to master the package mecanism: a class is found if the pakage names reflect a directory structure that appendded to any entry in the classpath gives the location of the class (in that package). If the class is in the default package it must be placed in any directory cited in the classpath.
The classpath, if not set, defaults to the current directory. Thus, compiling from mypackage directory the class B, which is in a package named mypackage, makes the compiler to looks for B in a non existing directory mypackage/mypackage. This is the entry in the classpath plus the package.
I placed A in test and B in test/mypackage . It compiled with "javac -classpath . mypackage.B" from test. The compiler found A in the current directory and B in test/mypackage.


SCJP2. Please Indent your code using UBB Code
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
However, is it impossible to import classses in nonpackage for classes in another packages?

It used to be possible to import classes in the default package into another package. However, beginning with jdk 1.4 it is no longer possible.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
John Pritchard
Ranch Hand

Joined: Nov 15, 2002
Posts: 49
Marilyn-
Thanks for the clarification. I was unaware of that change and that explains why I couldn't import classes even in the same directory.
This brings up a question for me. Does this type of nuance question appear on the 1.4 exam? e.g. importing non-package classes into packaged classes?
asdfasd
Greenhorn

Joined: Nov 07, 2002
Posts: 3
Originally posted by John Pritchard:
Surasak -
In your example, Class A has no access modifier therefore it is "default" e.g. classes of the same package can access it.

Hi
I'm unsurely about this statement. Class a has no access modifier?? I think, it is public. Right?
 
 
subject: a class in default invoked by a class in another package
 
Similar Threads
Classpath Doubt
Ch. 10 Development question
a question about package!
Plz help me in solving the problem of JAVAC comand
Classpath - Question 1