wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Packages/Subpackages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Packages/Subpackages" Watch "Packages/Subpackages" New topic
Author

Packages/Subpackages

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
I am assuming the subpackages are the packages inside packages. Are there any other rules for packages to be subpackages?

for eg:

com.javaranch.exams
com.javaranch.exams.scjp

package scjp is the subpackage of package exams.

Is this right?

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?

please explain.


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?
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
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)


SCJP 1.5(Done) SCJA 1.0(Done)<br />SCWCD(in Progress...)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.

Go figure.

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.


Mike Gershman
SCJP 1.4, SCWCD in process
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Thanks guys. I will try to find some more info on this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Packages/Subpackages