What is Package & how to create Package in JAVA?-vishmy1.blogspot.com

package

What is a Package?

A package is a set of classes and interfaces. It can be considered as a bundle of Java classes combined together to implement specific functionality. In Java, it is a mechanism to encapsulate a group of class, interface, and packages. It organizes class files into packages. In Java, there are already many predefined packages that we use while programming.

ex:- java.lang; , java.io; , java.util; , java.awt; and so on.

Advantage of Package:-


1)- Java package is used to categories the classes and interfaces so that they can be easily maintained.

2)- Java package provides access protection

3)- Java package removes naming collision.

4)- Application execution time is less.


 Types of Package:-

There are two types of package can be created.

1)- predifined package(builtin package):-

2)- user-defined package:-

1)- predefined package(default package or builtin package):-

The already defined package is known as a builtin package. java.lang, java.util, java.sql, etc are default package, which means the java package is automatically imported into the java program. this has classes that can be used for basic usage and system control.

2)- user-defined package:- 

The package that is created by the user is called a user-defined package.

Creating package:-

How do we create a package?

Syntax:- package package_name;

here package is a keyword followed by package name.

ex:- package pack;

Importing package:-

How do we import packages?

There are 3 different ways to access the package from outside.

1)- syntax:- import package_name;

import pack.*;

if you use package.* then all the classes and interface of this package will be accessible. but not subpackage.

ex:- 
import java.io.*;
import.java.*;

2)- import package_name.class_name;

The import keyword is used to make the classes and interface of another package accessible to the current package.

ex:- import java.util.scanner;

3)- fully qualified name:-

if we use a fully qualified name then only declare class of this package but no need of using import keyword. you need to use a fully qualified name every time when you access class or interface.

Syntax:- package_name.class_name object = new package.class_name();

Example of using Package:-

1)- First, create the necessary folder named pack. under this folder, we will create a class called mathematics.java.

What is Package & how to create Package in JAVA?-vishmy1.blogspot.com
pack folder

What is Package & how to create Package in JAVA?-vishmy1.blogspot.com
class
2)- Start writing code in mathematics.java.
code:-

public class mathematics 
{
    public void add_no(int a, int b)
    {
         int c = a+b;
         System.out.println(c);
    }
    public void sub_no(int x, int y)
    {
          int z = x+y;
          System.out.println(z);
    }
}

Note:- The above code is for the package which we have created as pack.

Now we will import the package (pack) which we have created.

Code:-

import pack.*;
class test
{
    public static void main(String args[])
    {
         mathematics m1 = new mathematics();
         m1.add_no(10, 20);
         m1.sub_no(20, 10);
     }
}


1)- A simple question arises is what if we do not declare/define any package in a program?

-The simple answer is java creates a default package in every program, if we do not declare/define any package in it.

-This package is called an anonymous package. and the default path of this package is in our "/bin" directory.

-While accessing these default package(anonymous package) class files, we do not need to import anything. these default package files are also by default accessible in every java program.

Note:-  Suppose we have multiple classes in the same package, we can use * to import all the public classes and interfaces from the package.




Post a Comment

0 Comments