• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Packages/Subpackages

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I will try to find some more info on this.
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic