java packageThe set of built in classes stored under certain name and used as a library in java application, is called as package.
The super most package of JAVA hierarchy is "java".

java.lang package :

This package include all the basic classes which are required every java application.

Examples :
  • object
  • string
  • System
  • Exception
  • Thread
  • etc...

Note : This package is by default allocated to every java program.

java.io package :

This package are used to handle the input and output operation between editor-console, client-server and database handling program.
Examples :
  • DataInoutStream
  • BufferedReader
  • fileOutputStream
  • fileReader
  • etc...

java.net package :

This package contains all the network related classes and interfaces. They are used to handle the networking in client-server architecture.

Examples :
  • Socket
  • ServerSocket
  • INetAddress
  • DatagramPackage
  • etc...

java.sql package :

This package have data handling classes and interface.

Examples :
  • class:
    • DriverManager
  • Interfaces:
  • Connection
  • Statement
  • ResultSet
  • etc...

java.awt package :

The classes of this package are used to represent the graphical output of java application.

Examples :
  • Button
  • Lable
  • Textfield
  • frame
  • Panel
  • Checkbox
  • etc...



java.awt.event package:

The interfaces of this package are used to handle the events on graphical components.

Examples :
  • ActionListener
  • TextListener
  • WindowsListener
  • MouseListener
  • etc...


keyword "import" is used to include any of the above package in java application.

Example:

import java.io.*;
This statement will include all the classes and interfaces of this package ("*" mean all the classes)
import java.fileReader;
This statement will include any fileReader class in program other classes are not included.
import java.awt.*;
This statement will inlcude only awt classes.
It cannot import the event package. If we want to import event package element then we have to write.
import java.awt.event.*;

How to create and use it in another application ?
Declare the keyword 'package' in the heading selection of package class.
Declare the fully fully qualified path of this class that where to store it in memory (Hard disk).
Declare all the elements as public in and with class. Save and compile this application in working folder.
Then create the folder as mention with package keyword. Cut the .class file of this class and paste it into the created package folder. This is the package creation process.
Create another java application in working folder. Include the package using import keyword and fully qualified path of package in new application, Now we can with it's elements.
//Package Code
package Abc.Outer.Inner.Pack1;
public class A //public class important
{
 public void fun1()
 {
  System.out.println("Hello package class method");
 }
}

Working :

my working folder
D:\xyz
save and compile

Create folders as mention with package keyword in working folder.
D:\xyz\Abc\Outer\Inner\pack1
cute and paste the A.class file into pack1 folder.

Now file location will be
D:\xyz\Abc\Outer\Inner\pack1\A.class

Create another java application in which this package class should be imported.

//Program code
class B
{
 public void fun2()
 {
  System.out.println("Hello fun2");
 }
 public static void main(String args[])
 {
  A a1=new A();
  a1.fun1();
  B b1=new B();
  a1.fun2();
 }
}


Another process to create package and store the .class file into it.

Use the -d attribute with javac command to create the directories. It creates the specified packages or directories with package keyword.

The "."
Directories should be created in working folders. And then the .class file of package class is stored in specified folder.

JAVAC -d . A.JAVA
-d for directors
. for working folder

Axact

Narendra

Hello, everyone! I am Narendra Bagul founder of Javanotes9. First of all thank you for visiting my blog. Hope you all like my java notes as well. I'm young part time blogger and a Computer Engineering student. I started blogging as a hobby. Now here I'm sharing my little acquired knowledge about my favorite programming language JAVA.

Post A Comment: