Packages Program in java
Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java packages?
Package: A package can be defined in Java programming language as a container in which sub-packages, interfaces, also classes are contained. Packages are also can be called a folder in a file directory.
The main purpose of these packages in this object-oriented programming language, Java, is to avoid naming conflicts. These packages are also used for organizations. For organizing sub-classes, interfaces and the classes which are project-related packages are used in the Java programming language.
Simply, we can say a package is a group which contains sub-packages, interfaces, also classes.
The main advantages of these Java packages are to provide access protection, for removing or else we can also say for avoiding naming conflicts when we are writing a program and categorize sub-packages, interfaces, also classes.
Packages can also be defined as the mechanism. And this mechanism in Java is used for encapsulation the things like sub-packages, interfaces, also classes.
We can also consider packages in Java programming language as data hiding or else data encapsulation also. With the help of packages in Java, we can make a search or else we can locate the usage of classes, annotations and also enumerations faster and easier while working with the Java programming language. As mentioned, packages provide access protection, a protected member in the Java program can be accessed by the classes in the sub-classes or else the in the same package in which it is defined. If we do not provide any access specifier, then the member defined is called a default member, and these default members can be accessed by classes which are defined in the same package only. So, we can say packages provide default and also protected access specifiers in the Java programming language.
Packages Types in Java Programming Language
- Built-in Packages
- User-defined Packages
Built-in Packages: Built-in packages are the packages from the Java Application Programming Interface (API – It is a software intermediary which helps in the communication of two applications to talk to each other). And Java API is a library of pre-defined interfaces, classes and also of sub-packages in Java. We know that a large number of classes are a part of the Application Programming Interface of Java, and all these classes will be in built-in packages.
Java.io, Java.awt, Java.net, Java.util, Java.applet, Java.lang,swing, net, sql are some of the built-in packages in Java programming language.
User-defined Packages: As the name indicates, user-defined packages are the packages which are defined by the user or can be created by the user while writing a program. The programmer who is working on the packages can freely create packages freely and also on their own. Here, the user will first create a package and then create MyClass inside the directory.
Defining a Package in Java
For defining a package in the Java programming language, we use the keyword “package” before the “package name”.
Rules for Defining Packages
- The first statement of the Java program which we are writing should be the package statement.
- It must be a single word. It should contain multiple words.
- It must and should follow camel case notation rules while defining the package names in the Java programming language.
The syntax for Defining a Package
Package packageName;
Let us understand the concept of packages in a Java programming language by seeing the below program.
package createdPackage;
public class void main(String[] args) {
System.out.println(“The class which we defined it belongs the “createdPackage");
}
}
For compiling the above code, use the below example syntax.
Javac -d. DefinedPackage.Java
So, the above command “Javac -d. DefinedPackage.Java” creates a directory with the package name “createdPackage”. Also creates “DefinedPackage.class”, which is saved into it.
Java createdPackage.DefinedPackage
The above command, "Java createdPackage.DefinedPackage” is the command which is used to run the program.
Importing the Packages
To import the Java packages, we use the keyword “import”. By using this keyword, we can import built-in packages and also user-defined packages.
syntax
import packageName.ClassName;
Below is the syntax for importing all classes at a time.
import packageName.*;
Let us take an example to understand the concept of importing the packages.
package packageCreated;
import Java.util.*;
public static void main(String[] args) {
scanner r = new Scanner(System.in);
int i = r.nextInt();
System.out.println(“We imported the package”);
}
}