• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

a class in default invoked by a class in another package

 
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.
 
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?
 
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.
 
Sheriff
Posts: 9099
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?
 
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?
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic