Java Packages - Creation And Use

Introduction 

 
A Java package is basically a grouping of files (Classes, Interfaces or Enumerated types). In other words, a package is an approach to organising files into different folders/directories according to their functionality, usability and category. It is your choice how to organise files. Another major reason for creating and using packages is removing name conflicts or collisions.
After creating a package, you can use it any of your projects. Even a package can contain many sub-packages. Thus, parent-child relationships are established among packages.
 

Syntax to create a package


Syntax
 
package package-name;
//Rest of the code here

 
It is so simple to create a package. You just need to write "package package-name;" as the very first statement in the Java source file as shown above. But, this is not it. You need to do a bit more. In general, packages are directories which contain classes or files. So, the Java source file you are using must be saved into the directory which must have the same name as a package name written in the Java source file.

Consider the following image:

java package

You can see that a file ("MyJavaProgram.java") and a folder ("package1") is available at a location ("C:\"). The package "package1" contains two classes, namely class1 and class2. class1.java and class2.java reside in package1; that means that the first line in class1.java and class2.java must be "package package1;", which declares that these classes are in package1.
 
Assume that class1 and class2 contain two methods method1() & method2() of the same name.

1. Now, you want to use these methods in "MyJavaProgram.java". You just need to import the package in the "MyJavaProgram.java" source as per your requirements, as shown below: 
  1. import package1.*; //To import all the classes in package1  
After creating a package, you can use it any of your projects. Even a package can contain many sub-packages. Thus, parent-child relationships are established among packages.
  
2. Now, you want to use these methods in "MyJavaProgram.java". You just need to import the package in the "MyJavaProgram.java" source as per your requirements, as shown below:
  1. import package1.*; //To import all the classes in package1.   
  2. import package1.class1; //To import class1 only, you can only able to access method of class1.   
  3. import package1.class2; //To import class2 only, you can only able to access method of class2.   
If there will be a subpackage in package1 you should write this line
  1. import package1.subpackage.*; //To import all classes from subpackage  .
Note: In order to import a package, the package must be available on the same location where your source file is, or you need to set the classpath.
 

Setting up Classpath

  • Open Command Prompt
  • Go to the directory where your source file is exist
  • Write the following command
  • set CLASSPATH=.;ClassPath;
where ClassPath is the path of the folder where your package exists; "." is used for the current directory and ";" is used to separate paths. If you don't specify "." in the command, then you may get "NoClassDefFoundError".
 

Classpath Example

 
Assume your Java source file is in drive "C:\" and the package is in "D:\Java\Packages\" then you should execute the following command before compiling and running the program:
 
C:\>set CLASSPATH=.;D:\Java\Packages\;
 

Learn with an Example

 
Create a package "checkstring" which contains a class "CheckString". There are various methods in "CheckString", listed below.
 
Method Description Action
IsLetters Verifies whether string contains only letters. Returns true if all characters in the string are letter(s), else returns false.
IsContainLower Verifies whether string contains any lower case letter. Returns true if string contains even a single lower case letter, else return flase.
IsContainUpper Verifies whether string contains any upper case letter. Returns true if string contains even a single upper case letter, else return flase.
IsContainNumber Verifies whether string contains any number. Returns true if string contains even a single number, else return flase.
IsContainSpecialChar Verifies whether string contains any special character. Returns true if string contains even a single special character, else return flase.
IsContainWhiteSpace Verifies whether string contains white space Return true if string contains white space(s), else return false.
 
Code (CheckString.java): Class resided in package "checkstring" 
  1. package checkstring;    
  2.     
  3. public class CheckString    
  4. {    
  5.     public boolean IsLetters(String str)   
  6.     {    
  7.         if (str.length() > 0)   
  8.         {    
  9.             int ASCII;    
  10.             for (int i = 0; i < str.length(); i++)   
  11.             {    
  12.                 ASCII = (int) str.charAt(i);    
  13.                 if ((ASCII < 65 || ASCII > 90) && (ASCII < 97 || ASCII > 122))   
  14.                 {    
  15.                     return false;    
  16.                 }    
  17.             }    
  18.             return true;    
  19.         }    
  20.         return false;    
  21.     }    
  22.     
  23.     public boolean IsContainUpper(String str)   
  24.     {    
  25.         if (str.length() > 0)   
  26.         {    
  27.             int ASCII;    
  28.             for (int i = 0; i < str.length(); i++)   
  29.             {    
  30.                 ASCII = (int) str.charAt(i);    
  31.                 if (ASCII > 64 && ASCII < 91)   
  32.                 {    
  33.                     return true;    
  34.                 }    
  35.             }    
  36.             return false;    
  37.         }    
  38.         return false;    
  39.     }    
  40.     
  41.     public boolean IsContainLower(String str)   
  42.     {    
  43.         if (str.length() > 0)   
  44.         {    
  45.             int ASCII;    
  46.             for (int i = 0; i < str.length(); i++)   
  47.             {    
  48.                 ASCII = (int) str.charAt(i);    
  49.                 return true;    
  50.             }    
  51.         }    
  52.         return false;    
  53.     }    
  54.     return false;    
  55. }    
  56.     
  57. public boolean IsContainNumber(String str)   
  58. {    
  59.     if (str.length() > 0)   
  60.     {    
  61.         int ASCII;    
  62.         for (int i = 0; i < str.length(); i++)   
  63.          {    
  64.             ASCII = (int) str.charAt(i);    
  65.             if (ASCII > 47 && ASCII < 58)   
  66.             {    
  67.                 return true;    
  68.             }    
  69.         }    
  70.         return false;    
  71.     }    
  72.     return false;    
  73. }    
  74.     
  75. public boolean IsContainSpecialChar(String str)   
  76. {    
  77.     if (str.length() > 0)   
  78.     {    
  79.         int ASCII;    
  80.         for (int i = 0; i < str.length(); i++)  
  81.         {    
  82.             ASCII = (int) str.charAt(i);    
  83.             if ((ASCII > 32 && ASCII < 48) || (ASCII > 57 && ASCII < 65) || (ASCII > 90 && ASCII < 97) || (ASCII > 122 && ASCII <= 255))   
  84.             {    
  85.                 return true;    
  86.             }    
  87.         }    
  88.         return false;    
  89.     }    
  90.     return false;    
  91. }    
  92. public boolean IsContainWhiteSpace(String str)   
  93. {    
  94.     if (str.length() > 0)   
  95.     {    
  96.         int ASCII;    
  97.         for (int i = 0; i < str.length(); i++)   
  98.         {    
  99.             ASCII = (int) str.charAt(i);    
  100.             if (ASCII == 32)   
  101.             {    
  102.                 return true;    
  103.             }    
  104.         }    
  105.         return false;    
  106.     }    
  107.     return false;    
  108.    }    
  109. }    
Code(MyProgram.java): Java program using thepackage"checkstring"
  1. import checkstring.CheckString;      
  2. public class MyProgram   
  3. {      
  4.     public static void main(String[] args)   
  5.     {      
  6.         CheckString obj = new CheckString();      
  7.         String str = "ThisIsMyNewTelephoneNumber";      
  8.         String str1 = "This is my new Telephone Number";      
  9.         String str2 = "+91-9400000000";      
  10.         if (obj.IsLetters(str) == true)   
  11.         {      
  12.             System.out.println(str + ", contains only letters.");      
  13.         }   
  14.         else   
  15.          {      
  16.             System.out.println(str + ", doesn't contains only letters.");      
  17.         }      
  18.         if (obj.IsLetters(str1) == true)   
  19.         {      
  20.             System.out.println(str1 + ", contains only letters.");      
  21.         }   
  22.         else   
  23.         {      
  24.             System.out.println(str1 + ", doesn't contains only letters.");      
  25.         }      
  26.         if (obj.IsContainWhiteSpace(str) == true)   
  27.         {      
  28.             System.out.println(str + ", contains white space(s).");      
  29.         }   
  30.         else   
  31.         {      
  32.             System.out.println(str + ",doesn't contains white space.");      
  33.         }      
  34.         if (obj.IsContainWhiteSpace(str1) == true)   
  35.         {      
  36.             System.out.println(str1 + ", contains white space(s).");      
  37.         }   
  38.         else   
  39.         {      
  40.             System.out.println(str1 + ",doesn't contains white space.");      
  41.         }      
  42.         if (obj.IsContainLower(str1 + str2) == true)   
  43.         {      
  44.             System.out.println(str1 + " " + str2 + ", contains lower case letter(s).");      
  45.         }   
  46.         else   
  47.          {      
  48.             System.out.println(str1 + " " + str2 + ", doesn't contain any lower case letter.");      
  49.         }      
  50.         if (obj.IsContainLower(str2) == true)   
  51.         {      
  52.             System.out.println(str2 + ", contains lower case letter(s).");      
  53.         }   
  54.         else   
  55.         {      
  56.             System.out.println(str2 + ", doesn't contain any lower case letter.");      
  57.         }      
  58.         if (obj.IsContainUpper(str1 + str2) == true)   
  59.          {      
  60.             System.out.println(str1 + " " + str2 + ", contains upper case letter(s).");      
  61.         }   
  62.         else   
  63.         {      
  64.             System.out.println(str1 + " " + str2 + ", doesn't contain any upper case letter.");      
  65.         }      
  66.         if (obj.IsContainUpper(str2) == true)   
  67.         {      
  68.             System.out.println(str2 + ", contains upper case letter(s).");      
  69.         }   
  70.         else   
  71.         {      
  72.             System.out.println(str2 + ", doesn't contain any upper case letter.");      
  73.         }      
  74.         if (obj.IsContainSpecialChar(str1 + str2) == true)   
  75.         {      
  76.             System.out.println(str1 + " " + str2 + ", contains special character(s).");      
  77.         }       
  78.         else   
  79.         {      
  80.             System.out.println(str1 + " " + str2 + ", doesn't contain any special character.");      
  81.         }      
  82.         if (obj.IsContainSpecialChar(str1) == true)   
  83.         {      
  84.             System.out.println(str1 + ", contains special character(s).");      
  85.         }       
  86.         else       
  87.         {      
  88.             System.out.println(str1 + ", doesn't contain any special character.");      
  89.         }      
  90.     }      
  91. }      
Output (MyProgram.java)
 
java package
 


Similar Articles