Package Java

Embed Size (px)

Citation preview

  • 8/22/2019 Package Java

    1/3

    JAVA: Creating a Simple Package

    Packages are the libraries in Java. They can be either pre-defined or user-defined. Creating

    packages follows a very simple mechanism. It is possible to use to include the same package

    command in more than one Java source file to indicate that all such classes belong to the samepackage.

    The following example shows how to create a simple package that has only one class. This classhas only one method test() whose objective is to determine if a given string is a palindrome or

    not. A palindrome as it is known is a set of characters that reads the same both ways. For

    instance, ABBA is a palindrome.

    This class is created under the mypackage package. This package later is imported in another

    class that is shown in a further example to make use of the method test().

    package mypackage;

    public class Palindrome

    {

    public boolean test(String str)

    {

    char givenstring[];

    char reverse[] = new char[str.length()];

    boolean flag = true;

    int count = 0, ctr = 0;

    givenstring = str.toCharArray();

    for(count = str.length() - 1; count >= 0; count++)

    {

    reverse[ctr] = givenstring[count];

    ctr++;

    }for(count = 0; count < str.length(); count++)

    {

    if(reverse[count] != givenstring[count])

    flag = false;

    }

    return flag;

    }

    }

    Save the code as Palindrome.java in the current working directory. Compile the program usingthe following command:

    javac -d . Palindrome.java

  • 8/22/2019 Package Java

    2/3

    It must be noted that there must be space before the . (dot) and another space after the dot. This

    creates a folder called mypackage in the current working directory and the class file is stored in

    mypackage directory.

    The CLASSPATH must be set to the current working directory so that the user-defined packages

    (class files) are available for use. Now that the package is created, it can be used in any Javaprograms. The import statement is used to include the newly created package in a program. This

    is illustrated in the next example:

    import mypackage.*;

    class Palintest

    {

    public static void main(String args[])

    {

    Palindrome objPalindrome = new Palindrome();

    System.out.println(objPalindrome.test(args[0]));

    }

    }

    In the above example, the user defined package is imported just as Javas built-in packages wereimported. An instance of Palindrome class is created since the method test() is not static. If the

    method test() was declared static then the method could have been invoked using only the class

    name and there would have been no need to create an instance of the class. The method test() is

    used just like any other built-in method.

    When a java program is executed, the JVM searches for the classes used within the program on

    the file system. Java uses one of the two elements to find a class:

    The package name The directories listed in the CLASSPATH environment variable.

    It must be noted that you must ensure that the Palindrome.java file is not located in the samedirectory as the Palintest.java file. The Palindrome.java file can be moved to the package folder

    mypackage.

    The output of the above folder is:

    The Palindrome.class file is located in the mypackage folder.

    Thus this can be concluded that the user-defined package mypackage can serve as a library for

    any other class.

  • 8/22/2019 Package Java

    3/3