Java Package


The package is used in Java conflict, to avoid name conflicts and to control the control of class, interface and counting etc. Package can be defined as a group of similar types of classes, interfaces, calculations, or sub-packages. Using the package makes it easy to find related classes and it is a good for projects with hundreds of classes and other files. Provides structure.

Types of Packages: 

Built-in and User defined

Built-in package: for example the existing Java package java.lang, java.util etc.
User-defined-package: Java package created by user to classify the sections and interface of your project.

Creating a package

While creating a package, the programmer will have to choose a name for the package and at the top of that source, a package statement must be included in the name that includes the class, interface, enumeration and annotation types that you want to include in the package . Only one statement of the package can be given in each source code, and it applies to all types of files.

To compile the Java programs with package programmers have to do used option as shown below


javac -d Destination_folder file_name.java

package mypack;
public class employee 
{
    statement; 
}
//save as FirstProgram.java  
package learnjava;  
public class FirstProgram{  
    public static void main(String args[]) {  
        System.out.println("Welcome to package");  
    }  
}

 implementing Package

package weapons;
interface ammo {
 public void sniper();
 public void rifle();
}


Accessing Package

There are three ways to access the package from outside the package:
  • import package.*;
  • import package.classname;
  • fully qualified name.
When you import many contexts of a particular package, you can use the import statement. The same approach can also be used to access the user-defined packages. Import statements can be used to search the list of packages for a particular category. The usual form of import statement for the search of a class is:

Import package 1. package2] [. package3] .classname;

Here, the name of package 1 top-level package is named Package 2 package which is inside 1 package i.e. Package 1 and similar. Programmers can use any number of packages in the package hierarchy. Finally, the class name is clearly specified.



Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment