Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Ashan Kaya   on Nov 21 In Java Category.

  
Question Answered By: Lewis Welch   on Nov 21

Before you use packages  you must know Java's access specifiers are
public, private, and protected. Java also defines a default access
level. protected applies only when inheritance is involved. The other
access specifiers are described next.




Declarations and Access Control - Access Modifiers
====================================================

public Modifier
****************
Used with:
-----------
Classes,Interfaces,Constructors,Inner Classes,Methods, Field Variables


Description
------------
A Class or Interface may be accessed from outside its package.
Constructors, Inner Classes, Methods and Field variables may be
accessed from wherever their class is accessed.

protected Modifier
*******************
Used with:
-----------
Constructors,Inner Classes,Methods,Field variables

Description
------------
May be accessed by other classes in the same package or from any
subclasses of the class in which they are declared.

private Modifier
****************
Used with:
-----------
Constructors,Inner Classes,Methods,Field variables

Description
------------
May be accessed only from within the class in which they are declared.

No(Default) Modifier
****************
Used with:
-----------
Classes,Interfaces,Constructors,Inner Classes,Methods,Field variables

Description
------------

May only be accessed from within the package in which they are declared.

===================================================================
Packages - Defining
*****************

Package = directory. Multiple classes of larger programs are usually
grouped together into a package. Packages correspond to directories in
the file system, and may be nested just as directories are nested.

Reasons to use packages

* Grouping related classes.
* In large programs prevents name conflicts.
* Allows effective use of default "package" visibility.
* Creates a unique class name for distribution, Preferences, ...

Grouping related classes. If you are writing a normal one-programmer
application, it will typically be in one package. The purpose of a
package is to group all related classes together. If you are working
on a program that is divided into separate sections that are worked on
by others, each section might be in its own package. This prevents
class name conflicts and reduces coupling by allowing package scope to
be used effectively.

Library in package. If you're writing a library to use in various
programming projects or by others, put it in its own package. Programs
that use this library will be developed in their own packages. With
its thousands of library classes, Java has grouped related library
classes into packages, but usually you won't define more than one package.
Summary of how many packages you usually define

* Normal programs = one package.
* Library = one package.
* Program + library = two packages.

Reasons for using packages

* Limit the scope of class names.
* To make package access more usable.

Package declarations

Each file may have a package declaration which precedes all
non-comment code. The package name must be the same as the enclosing
directory.

Default package. If a package declaration is omitted, all classes in
that directory are said to belong to the "default" package.

Here are two files in the packagetest directory.

package packagetest;

class ClassA {
public static void main(String[] args) {
ClassB.greet();
}
}




package packagetest; // Same as in previous file.

class ClassB {
static void greet() {
System.out.println("Hi");
}
}

Note that these source files must be named ClassA.java and ClassB.java
(case matters) and they must be in a directory named packagetest.


Compiling and running packages from a command line

To compile  the above example, you must be outside the packagetest
directory. To compile the classes:

javac packagetest/ClassB.java
javac packagetest/ClassA.java

To run  the main program in ClassA.

java packagetest.ClassA

or

java packagetest/ClassA

In windows the "/" can be replaced by the "\" in the javac command,
but not in the java command. Generally use a forward slash ("/")
because it is used more commonly than the backslash in other places as
well.
<!-- If you've been developing small programs, you didn't have to
worry about grouping all your files. A "package" corresponds to a
directory, and they are sometimes nested for a large number of files,
like the Java library. If you look at the Java library source code,
you'll find a directory called "java". In that directory, you'll find
a "util" directory (the java.util package) which contains all the
classes in this package (Scanner, ArrayList, etc), an "awt" directory
(java.awt package), etc. The other reason to define packages is to
make your fully qualified class names completely unique. Java
recommends using a reversed domain name. I own the domain name
fredswartz.com, so I often use the package name com.fredswartz, at
least for any programs that I'm moderately serious about. Sun
recommends strongly that you use packages for your programs -- I
believe this is because it's difficult to do some things if they
aren't in a package, or at least it's more convenient (eg,
Preferences). Nothing you'd use for a small program, but it becomes
important for larger programs. -->

===========================================================


CLASSPATH
*************
CLASSPATH tells Java where to search for programs

Where to look? The Java runtime system needs to know where to find
programs that you want to run and libraries that are needed. It knows
where the predefined Java packages are, but if you are using
additional packages, you must tell specify where they are located.

CLASSPATH. Specifying where to search for additional libraries in
Windows is easily done by seeting the environment variable CLASSPATH,
which Java uses to see where it should find Java programs and libraries.


The classpath  variable can be set  on Windows XP with the following steps.

* Click the Start button in the lower left of the screen.
* Select Control Panel from the pop-up menu.
* Choose System from the submenu.
* Click the Advanced tab.
* Click the Environment Variables button near the bottom and you
will see two lists of variables.
* Look in the System variables list for a variable named
CLASSPATH. If you find it, click Edit. If you don't find it, click New
and enter CLASSPATH in the Variable name field.
* The Variable value field is a list of file paths of directories
or jar files. The first thing in this list should be the current
directory, which is represented in windows just as it is in Unix, as a
single period. If there's more than one thing in this list, separate
items with a semicolon. For example, my CLASSPATH variable starts with
these three items (there are a couple more, but this should be enough
to give you the idea). The only part you need is the first "dot".
variable Value
-------- ------
CLASSPATH .;C:\packagetest;c:\classpath\TableLayout.jar;

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on what are "PACKAGES" Or get search suggestion and latest updates.


Tagged: