Also, is there anything different as for declaring a package as subpackage other than structuring:
for ex: importing a package, does it import the sub package as well?
This is based on the following question on danchisholm
Which of the following statements are true? a. By default assertions are disabled at run-time. b. Assertions can be selectively enabled for any named class. c. If assertions are selectively enabled for a named class then assertions are automatically enabled for any subclass of the named class. d. Assertions can be selectively enabled for any named package. e. If assertions are selectively enabled for any named package then assertions are automatically enabled for the subpackages. f. Assertions can be selectively enable for the unnamed package in the current working directory. g. Assertions can be selectively enable for any unnamed package in any named directory.
The answers given are a.b.d.e.f
Is this right?
Also, what is unnamed package in current working directory vs. named directory?
at first question, you are right, that is the way of declaring packages subpackages are the packages inside packages. now when you do import, remember that you are importing classes not packages, so if for instance : package A has B,C,and D packages inside when you import everything in package A by doing import A.*; you are importing all the classes in that package everything else is ignored that means other packages(directories in this instance its same)
Java packages are funny things. Sometimes they are treated as a hierarchy based on names separated by ".", other times each package name is just a string of characters and the "." means nothing special.
The "java -ea "x.y..." option is an example of the first case. It enables assertions in classes in package "x.y", package "x.y.z", and package "x.y.z.w".
Similarly, the classes in a package are placed in a directory tree based on the package names. In this sense, x.y is a subpackage of x.
On the other hand, "import x;" has no effect on classes in package x.y.
The unnamed package consists of all classes whose source programs don't start with a package statement. These classes don't have to be in any particular directory, but their directories must be in the CLASSPATH, which can include ".", the current working directory at the time of execution.
I think the term "named directory" means a directory whose name is in the classpath. This may also be the "current working directory", so the terms are not mutually exclusive. You can enable assertions in all classes in the current working directory which are in the unnamed package with "java -ea ...". There is no syntax to enable the classes in the unnamed package in a named directory.
SCJP 1.4, SCWCD in process
Joined: Mar 04, 2005
Thanks guys. I will try to find some more info on this.