» » Packages in java

Package:

  • Packages are nothing but all the related classes and interfaces and .class files.
  • Packages are the folders which are binding all the related ".class " files together
  • Binding: making something available to related functionalities.
  • A simple folder would only groups all the related files . But where as packages are also folders which binds all the related files.
  • Every package would be a folder. But every folder can not be a package

How to create user defined package:

  • By using a keyword "package".
  •  package package_name.
  1. package instanceofjavaforus;
  2. Class Demo{
  3. public static void main (String args[]) {
  4. }
  5. }

 

The Directory Structure of Packages:

  • To make java compiler to crate a package for us and automatically load all corresponding files into package
  1. package instanceofjavaforus;
  2. public Class Demo{
  3. public static void main (String args[]) {
  4. }
  5. }
  • Save this as Demo.java 
  • At this time there is no folder with name instanceofjavaforus 
  •  Javac -d . Demo.java.
  • -d : creates directory
  • instanceofjavaforus -> Demo.java: a folder with name instanceofjavaforus will be created and inside Demo.class will be there.

Creating Sub packages:

  • Create a package with a class;
  1. package pack;
  2. public  Class Demo{
  3. public static void main (String args[]) {
  4. }
  5. }
  •  C:/> javac -d . Demo.java
  • Now pack-> Demo.class folder structure will be created.
  • Now create sub package like this.

  1. package pack.subpack;
  2. public  Class Abc{
  3. public static void main (String args[]) {
  4. }
  5. }
  •  c:/> javac -d . Abc.java
  • pack->subpack -> Demo.class,Abc.class 

 How to create jar file representing our package: 

  •  By using dos command
  • c:/> jar -cvf file_name package_name.
  • Ex: jar -cvf instanceofjava.jar pack

Importing a package:

  • We can import packages by using "import".
  • To import the package
  • import<space><package-name><.><* or name of class you want to use from that package
  • To import particular class inside a package>.
  • import<space><package-name><.><name of class you want to use from that package>
  • As per the naming convention: For  package names use  small case letters.
  1. import pack.Demo;
  2. public  Class Xyz extends Demo {
  3. public static void main (String args[]) {
  4. }
  5. }



  1. import pack.*;
  2. public  Class Xyz extends Demo {
  3. public static void main (String args[]) {
  4. }
  5. }

 

Commonly using Predefined packages in java:  

Java.lang:

Provides classes that are fundamental to the design of the Java programming language. like
  1. Integer
  2. boolean
  3. Class
  4. Object
  5. Runtime
  6. Thread
  7. System
  8. Process
  9. Package
  10. String

Java.util.*:

This package contains the collections framework classes, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous. some of the interfaces and classes present in java.util package are as follows.
  1. Collection
  2. Comparator
  3. Enumeration
  4. RandomAccess
  5. List
  6. Set
  7. Map
  8. Iteraot
  9. EventListener
  10. Scanner (class)

Java.io.* :

This package provides classes and interfaces for system input and output through data streams, serialization and the file system.Some of the classes present in java.io package are as follows.
  1. BufferedInputStream
  2. BufferedOutputStream
  3. BufferedReader
  4. SerializablePermission
  5. Console
  6. File
  7. FileReader
  8. FilerWriter
  9. InputStream
  10. PrintStream

Java.text.* :

This package is used for formatting date and time on day to day business
operations.Some of the classes present in java.text package are as follows
  1. Annotation
  2. Collater
  3. Format
  4. DateFormat
  5. DecimalFormat
  6. MessageFormat
  7. NumberFormat
  8. Normalizer
  9. RuleBasedCollator
  10. ParsePosition

Java.sql.* 

This package is used for retrieving the data from data base and performing
various operations on data base. here some of the classes present in java.sql.
  1. Drivermanger
  2. Date 
  3. Time
  4. SqlPermission
  5. Timestamp
some of the interfaces present in java.sql package
  1. CallableStatement
  2. Connection
  3. PreparedStatement
  4. ResultSet
  5. Statement
  6. SQLData
  7. SQLInput
  8. SQLOutput

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu