A Java package is basically a grouping of files (Classes, Interfaces or Enumerated types). In other words, a package is the 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 package
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 contains 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:
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.
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:
import package1.*; //To import all the classes in package1.
import package1.class1; //To import class1 only, you can only able to access method of class1.
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
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 class path.
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"
package checkstring;
public class CheckString {
public boolean IsLetters(String str) {
if (str.length() > 0) {
int ASCII;
for (int i = 0; i < str.length(); i++) {
ASCII = (int) str.charAt(i);
if ((ASCII < 65 || ASCII > 90) && (ASCII < 97 || ASCII > 122)) {
return false;
}
}
return true;
}
return false;
}
public boolean IsContainUpper(String str) {
if (str.length() > 0) {
int ASCII;
for (int i = 0; i < str.length(); i++) {
ASCII = (int) str.charAt(i);
if (ASCII > 64 && ASCII < 91) {
return true;
}
}
return false;
}
return false;
}
public boolean IsContainLower(String str) {
if (str.length() > 0) {
int ASCII;
for (int i = 0; i < str.length(); i++) {
ASCII = (int) str.charAt(i);
return true;
}
}
return false;
}
return false;
}
public boolean IsContainNumber(String str) {
if (str.length() > 0) {
int ASCII;
for (int i = 0; i < str.length(); i++) {
ASCII = (int) str.charAt(i);
if (ASCII > 47 && ASCII < 58) {
return true;
}
}
return false;
}
return false;
}
public boolean IsContainSpecialChar(String str) {
if (str.length() > 0) {
int ASCII;
for (int i = 0; i < str.length(); i++) {
ASCII = (int) str.charAt(i);
if ((ASCII > 32 && ASCII < 48) || (ASCII > 57 && ASCII < 65) || (ASCII > 90 && ASCII < 97) || (ASCII > 122 && ASCII <= 255)) {
return true;
}
}
return false;
}
return false;
}
public boolean IsContainWhiteSpace(String str) {
if (str.length() > 0) {
int ASCII;
for (int i = 0; i < str.length(); i++) {
ASCII = (int) str.charAt(i);
if (ASCII == 32) {
return true;
}
}
return false;
}
return false;
}
}
Code (MyProgram.java): Java program using the package "checkstring"
import checkstring.CheckString;
public class MyProgram {
public static void main(String[] args) {
CheckString obj = new CheckString();
String str = "ThisIsMyNewTelephoneNumber";
String str1 = "This is my new Telephone Number";
String str2 = "+91-9400000000";
if (obj.IsLetters(str) == true) {
System.out.println(str + ", contains only letters.");
} else {
System.out.println(str + ", doesn't contains only letters.");
}
if (obj.IsLetters(str1) == true) {
System.out.println(str1 + ", contains only letters.");
} else {
System.out.println(str1 + ", doesn't contains only letters.");
}
if (obj.IsContainWhiteSpace(str) == true) {
System.out.println(str + ", contains white space(s).");
} else {
System.out.println(str + ",doesn't contains white space.");
}
if (obj.IsContainWhiteSpace(str1) == true) {
System.out.println(str1 + ", contains white space(s).");
} else {
System.out.println(str1 + ",doesn't contains white space.");
}
if (obj.IsContainLower(str1 + str2) == true) {
System.out.println(str1 + " " + str2 + ", contains lower case letter(s).");
} else {
System.out.println(str1 + " " + str2 + ", doesn't contain any lower case letter.");
}
if (obj.IsContainLower(str2) == true) {
System.out.println(str2 + ", contains lower case letter(s).");
} else {
System.out.println(str2 + ", doesn't contain any lower case letter.");
}
if (obj.IsContainUpper(str1 + str2) == true) {
System.out.println(str1 + " " + str2 + ", contains upper case letter(s).");
} else {
System.out.println(str1 + " " + str2 + ", doesn't contain any upper case letter.");
}
if (obj.IsContainUpper(str2) == true) {
System.out.println(str2 + ", contains upper case letter(s).");
} else {
System.out.println(str2 + ", doesn't contain any upper case letter.");
}
if (obj.IsContainSpecialChar(str1 + str2) == true) {
System.out.println(str1 + " " + str2 + ", contains special character(s).");
} else {
System.out.println(str1 + " " + str2 + ", doesn't contain any special character.");
}
if (obj.IsContainSpecialChar(str1) == true) {
System.out.println(str1 + ", contains special character(s).");
} else {
System.out.println(str1 + ", doesn't contain any special character.");
}
}
}
Output (MyProgram.java)