Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a class in default invoked by a class in another package

 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer.
However, is it impossible to import classses in nonpackage for classes in another packages?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Pritchard
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic