What is a Package in Java?

  • Thread starter Katerina MacLean
  • Start date
K

Katerina MacLean

What is a package in java?

My assumption is that it is a collectyion of classes that are compiled
together and have dependency - is this correct?
 
T

Tor Iver Wilhelmsen

Katerina MacLean said:
What is a package in java?

Basically it's what e.g. C++ and C# calls a "namespace", except it's
also reflected in hierarchical structures on the classpath.
 
C

Chris Smith

Katerina said:
What is a package in java?

My assumption is that it is a collectyion of classes that are compiled
together and have dependency - is this correct?

Not exactly, no. There is no requirement that classes in the same
package are compiled together, nor that they have any dependencies
between them. Both are likely, but in the end a package is basically
just a prefix at the beginning of the name of a class. As an example,
there exists a class called org.w3c.dom.Document, which is a part of the
DOM recommendation as implemented in the core Java API. It can also be
described as the class in package "org.w3c.dom" with a simple name of
"Document".

The primary purpose of a package is to avoid naming conflicts. For
example, there is also a class called Document in the javax.swing.text
package. These two classes have completely different purposes, and you
can tell them apart because of their different package names. If you
only intend to use one of them, then you can use the "import" statement
to tell the compiler that when you write "Document", you actually mean
"org.w3c.dom.Document" and not "javax.swing.text.Document" Of course,
you can always use the full names if, for example, you need to use both
Document classes in the same source file.

Packages also play a role in access control specifiers and in the Java
security architecture with package sealing. These are secondary to the
main purpose of a package.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

djaksic said:
Almost corect, also there are .java files included.

[Top-posting fixed]

I'm not sure where you're getting that from. Files have nothing to with
packages. An implementation of Java such as IBM's old Visual Age
product, which did not store source code in files at all, still would
need to have packages because they are a part of the Java language.

The statement that a ".java" file is included in a package is about
equivalent to saying that protective plastic coating on a distribution
CD is a part of the Windows operating system. The two concepts exist at
different levels, and can't be compared.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
I

iamfractal

Chris Smith said:
Riippp ...

The primary purpose of a package is to avoid naming conflicts. Teeaaarrr ...

Packages also play a role in access control specifiers and in the Java
security architecture with package sealing. These are secondary to the
main purpose of a package.


Hi, Chris!

Yes, a namespace is a collection of unique names, and a package is the
Java implementation of a namespace, but it seems odd to describe this
as the primary use of packages (let's call this the name-collision use).

Surely, that packages encapsulate closely-related classes from other,
less closely-related classes is the primary use of packages in Java
(let's call this the encapsulation-use).

To take an extreme example: let's say we have 100 classes, three of
which have the same name, "Sausage." If the naming-collision use were
the primary use, then we could divide our system into three packages:
one will have 98 classes, and other two will have one class each.

Most Java I have seen, however, divides the system into packages in
which the implementation of related functionality is hidden away from
the rest of the system; the package name is sort-of a short-hand for
the contained functionality.

OK, so this is so extreme as to be stupid: and you said that the
name-collion use was the, "Primary," use, not the, "Only," use. But,
from your signature, you're a Java-course designer: so if you're
teaching kids Java and you could only tell them one use for Java
packages, would you tell them the name-collision use, or the
encapsulation-use? And doesn't this decide what use is primary?

May your battles be as savage as they are victorious, as our Klingon friends say,

..ed

www.EdmundKirwan.com - Home of The Fractal Class Composition
 
C

Chris Smith

Hi, Chris!

Hi.

Yes, a namespace is a collection of unique names, and a package is the
Java implementation of a namespace, but it seems odd to describe this
as the primary use of packages (let's call this the name-collision use).

Indeed, I can see that I caused some confusion. For whatever reason, I
was thinking in language lawyer mode at the time I write that.
Certainly there are a number of justifications for the use of packages,
and grouping related classes together is an important one. It didn't
seem as important at the time because it isn't a functional use of
package, but rather a communicative use.

In the end, the two are related anyway, and it encompasses both to say
that packages group related classes together and in so doing, they
provide some context to qualify the class names.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Sipayi

Sometimes the simplest ideas of Java are never understood.

I have seen some coding conventions of Java in a multi-million
dollar *J2EE* project of a telecom giant. As per the coding
conventions, ever class name in the project is prefixed by 13
characters, AAA_BBBB_CCCC_<class name>
AAA for the project name, BBBB for the module and CCCC for
classifying the class into utility/shared/business related/etc.,

<sigh>
-Siplin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top