Packages in java.|| what are packages in java explain with example?

                         Packages In Java

Packages.
The package is both a naming and a visibility control mechanism. You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are only exposed to other members of the same package. This allows your classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world.
Defining a Package
To create a package is quite easy: simply include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If you omit the package statement, the class names are put into the default package, which has no name.

Syntax
package myPackage;

File System Directories And Packages
Java uses file system directories to store packages. For example, the .class files for any classes you declare to be part of MyPackage must be stored in a directory called MyPackage. Remember that case is significant, and the directory name must match the package name exactly.

Multiple files having same package statement

More than one file can include the same package statement. The package statement simply specifies to which package the classes defined in a file belong. It does not exclude other classes in other files from being part of that same package. Most real-world packages are spread across many files.


Packages Hierarchy
You can create a hierarchy of packages. To do so, simply separate each package name from the one above it by use of a period. The general form of a multileveled package statement is shown here:
package pkg1[.pkg2[.pkg3]];
A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as
package java.awt.image;
needs to be stored in java/awt/image, java\awt\image, or java:awt:image on your UNIX, Windows, or Macintosh file system, respectively. Be sure to choose your package names carefully. You cannot rename a package without renaming the directory in which the classes are stored.

Finding Packages and CLASSPATH
How does the Java run-time system know where to look for packages that you create? The answer has two parts. First, by default, the Java run-time system uses the current working directory as its starting point. Thus, if your package is in the current directory, or a subdirectory of the current directory, it will be found. Second, you can specify a directory path or paths by setting the CLASSPATH environmental variable.


Package Example:

// myPackageTest.java
package myPackage;
class A
{
void test()
{
System.out.println("This is test method of class A");
}
}
class B
{
void test()
{
System.out.println("This is test method of class B");
}
}
class myPackageTest
{
public static void main(String args[])
{
A objA = new A();
objA.test();
B objB = new B();
objB.test();
}
}


Note:
Create a folder named as ―myPackage‖ in yours current working folder save above file named as ―myPackageTest.java‖ in that folder. Then compile this file it will creates a resulting ―myPackageTest.class‖ in ―myPackage‖ folder.
Then try executing the ―myPackageTest.class‖ using the following command line:
java MyPack.myPackageTest
Remember, you will need to be in the directory above ―myPackage‖ when you execute this command, or to have your CLASSPATH environmental variable set appropriately.
As explained, ―myPackageTest‖ is now part of the package ―myPackage‖. This means that it cannot be executed by itself. That is, you cannot use this command line:
java myPackageTest

Access Protection

Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access.

protected

If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected.

Class Access

A class has only two possible access levels: default and public. When a class is declared as public, it is accessible by any other code. If a class has default access, then it can only be accessed by other code within its same package.




Importing Packages


There are no core Java classes in the unnamed default package; all of the standard classes are stored in some named package. Since classes within packages must be fully qualified with their package name or names, it could become tedious to type in the long dot-separated package path name for every class you want to use. For this reason, Java includes the import statement to bring certain classes, or entire packages, into visibility. Once imported, a class can be referred to directly, using only its name.

In a Java source file, import statements occur immediately following the package statement (if it exists) and before any class definitions. This is the general form of the import statement: import pkg1[.pkg2].(classname|*);

Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package inside the outer package separated by a dot (.). There is no practical limit on the depth of a package hierarchy, except that imposed by the file system. Finally, you specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package.

All of the standard Java classes included with Java are stored in a package called java. The basic language functions are stored in a package inside of the java package called java.lang. Normally, you have to import every package or class that you want to use, but since Java is useless without much of the functionality in java.lang, it is implicitly imported by the compiler for all programs.

If a class with the same name exists in two different packages that you import using the star form, the compiler will remain silent, unless you try to use one of the classes. In that case, you will get a compile-time error and have to explicitly name the class specifying its package.

e.g   import java.util.Date; OR import java.io.*;



Post a Comment

Please do not enter any spam link in the comment box