Logo

dev-resources.site

for different kinds of informations.

What are Packages and How we can use it?

Published at
11/1/2023
Categories
java
packages
datastructures
productivity
Author
believer15
Author
10 person written this
believer15
open
What are Packages and How we can use it?

In this article, We will learn about packages and What is the hierarchy and learn about how Packages contains different class and methods. This term packages used in every programming languages, so I though to share my knowledge and some examples to related to topic.

With term Packages what comes in our mind. Just think once and let me know in the comment. So, what you think that's right!!!

Image description

Example :- Packages is same as you might think in general wrapper or box.

But What we say in technical term, What are Packages?

Something that's Packed Right!!!

Packages is set of programs files and data files which developer by programmers to perform specific task and include information regarding installation of packages.

Or We can say...

A package is a container of a group of related classes where some of the classes are accessible are exposed and others are kept for internal purpose.
We can reuse existing classes from the packages as many time as we need it in our program.

Here is the hierarchy of Packages -> Sub-Packages-> classes
You can visualize how it is structured inside single package java.

Image description

Image description

Now, You had a Clear understanding of the what are packages and how it is structured. Now, We move forward and go deep down in packages and it's type? How it works?

Types of Packages:-

Image description

User Defined Packages

These are the packages that are defined by the user. First we create a directory myPackage (name should be same as the name of the package). Then create the MyClass inside the directory with the first statement being the package names.

for Example :-

// Name of the package must be same as the directory
// under which this file is saved
package myPackage;

public class MyClass
{
    public void getNames(String s)
    {        
        System.out.println(s);        
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, we can use above MyClass class in our program.

/* import 'MyClass' class from 'names' myPackage */
import myPackage.MyClass;

public class PrintClass 
{
   public static void main(String args[]) 
   {       
      // Initializing the String variable with a value 
      String name = "Casto";

      // Creating an instance of class MyClass in the package.
      MyClass obj = new MyClass();

      obj.getNames(name);
   }
}
Enter fullscreen mode Exit fullscreen mode

Note :- MyClass.java must be saved inside the myPackage directory since it is a part of the package.

Builtin Packages

BuiltIn Packages consist of large number of classes which is defined by java or certain programming language to perform necessary operations.
Built-in packages in Java consist of classes that are part of the Java API.

Some of the commonly used built-in packages used in java are:-

1) java.lang:- Contains language support classes.This package is automatically imported.
2) java.io:- Contains classed for supporting input / output operations.
3) java.util:- Contains utility classes which implement data structures like Linked List, Dictionary and support,for Date / Time operations.
4) java.applet:- Contains classes for creating Applets.
5) java.awt:- Contain classes for implementing the components for graphical user interfaces (like button , ;menus etc).
6) java.net:- Contain classes for supporting networking operations.

Using Static import

Static import is a feature introduced in Java programming language ( versions 5 and above ) that allows members ( fields and methods ) defined in a class as public static to be used in Java code without specifying the class in which the field is defined.

import static java.lang.System.*; 

class ImportDemo 
{ 
public static void main(String args[]) 
{    
        // We don't need to use 'System.out' 
        // as imported using static. 
        out.println("GeeksforGeeks"); 
} 
} 
Enter fullscreen mode Exit fullscreen mode

Handling Name Conflicts

The only time we need to pay attention to packages is when we have a name conflict . For example both, java.util and java.sql packages have a class named Date. So if we import both packages in program as follows:

import java.util.*;
import java.sql.*;

//And then use Date class, then we will get a compile-time error :

Date today ; //ERROR-- java.util.Date or java.sql.Date?
Enter fullscreen mode Exit fullscreen mode

The compiler will not be able to figure out which Date class do we want.
This problem can be solved by using a specific import statement:

import java.util.Date;
import java.sql.*;
Enter fullscreen mode Exit fullscreen mode

If we need both Date classes then, we need to use a full package name every time we declare a new object of that class.
For Example:

java.util.Date deadLine = new java.util.Date();
java.sql.Date today = new java.sql.Date();
Enter fullscreen mode Exit fullscreen mode

Important Points to Remember :-

1- Every class is part of some package.
2- If no package is specified, the classes in the file goes into a special unnamed package (the same unnamed package for all files).
3- All classes/interfaces in a file are part of the same package. Multiple files can specify the same package name.
4- If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
5- We can access public classes in another (named) package using: package-name.class-name

packages Article's
30 articles in total
Favicon
Exploring the Coastal Beauty of the Turkish Riviera
Favicon
Simplifying State Management in Laravel: Managing Transitions with Enum StateΒ Machine
Favicon
Task-Python Packages
Favicon
Agra Honeymoon Packages
Favicon
Agra Family Packages
Favicon
Testando e Implementando Pacotes Laravel: Um Guia Passo a Passo
Favicon
Popular Packages for Express.js
Favicon
Discover Paris: Your Ultimate Tour Package from Chennai
Favicon
Learn to create your own ROS2 Workspace, Custom Packages, Publisher and Subscriber.
Favicon
Publicando seus packages no pub.dev
Favicon
How I wrote a package manager in JavaScript
Favicon
Easily Create Your Own Private NPM Registry Using Verdaccio
Favicon
Laravel Deployer Free package for laravel and nodejs apps Deployment
Favicon
Embark on a Spiritual Journey: Umrah Group Packages for 2024
Favicon
Jazz Monthly Data Package
Favicon
Georgia Travel Tips for First-time Visitors
Favicon
The one thing I do not like about the Nix package manager (and a fix for it)
Favicon
Can't find Process Explorer after installing it via winget? Do this
Favicon
Develop anywhere - A practical guide to using packages with webMethods.io
Favicon
My process for writing Laravel packages
Favicon
What are Packages and How we can use it?
Favicon
Best Kerala Honeymoon Packages From Seasonz India Holidays
Favicon
Creating and Publishing Dart Packages for Flutter
Favicon
Backup manually installed libraries and packages in Ubuntu
Favicon
Unleashing the Power of Julia: Top 5 Must-Have Packages
Favicon
NPM Vs. Yarn: How do they compare?
Favicon
Recalling NPM Commands to Your Heart's Content
Favicon
Python generated jokes πŸ˜‚πŸ€£
Favicon
Problems with packages in Go
Favicon
The hidden cost of packages and how to avoid it

Featured ones: