Hello World! Today’s blog is about packages in java. Here, we will learn about what are packages and how we can use packages along with syntax and examples.
Definition For Packages In Java
Packages in java are collection of classes, interfaces (or) other packages. They are used for organizing java project. The primary purpose of using packages is to avoid naming conflicts and make it easier to manage code.
When we are creating a package, the name should be written in camelCase style.
To access the package from different packages, you need to use the “import” keyword:
Syntax For Importing Packages In Java
We can import packages from another packages in three different types:
Type 1: Importing A Certain Class Only
import java.util.Test; // if you want only certain class we can import like this.
Example for Type 1
Related Topics:
Check Array Formation Through Concatenation | Leetcode Problem 1640
Type 2: Accessing Any Class Within This Package
import java.lang.*; // In this type “*” means we can use any class from this package
Example for type 2
Type 3: Directly Create Object Without Importing Any Package
Here we are not importing any kind of packages. We will write it directly when we are creating an object.
Example for type 3
So that’s it for todays topic, we will reconnect with another interesting topic.
If there are any queries in this topic please let me know in the comments.