Packages in Java with Examples…!!!

Packages….!!!Packages are those class which contains methods without the main method in them.Packages are mainly used to reuse the classes which are already created/used in other program.We can define many number of classes in the same package.Packages are mainly divided into two types.They are
1.Built in packages
2.User defined Packages.
The main use of Packages is:

  1. Classses contain in packages of other programme can easily be used.
  2. In packages,classes can be unique,compare with classes in other packages.
  3. Packages provide the way to hide classes thus preventing other program(or) packages from accessing classes that are meant for internal use only.

Built in Packages:Built Packages are provided for the programme in-order to reduce the burden.Some of the frequently used built in packages are:

  1. lang
  2. util
  3. io
  4. awt
  5. net
  6. applet

These built in packages are placed in the package “java”.In order to use these packages/classes present in the package we need to import them in the current program.The syntax for importing packages is:
import java.util.*
The above statement tells the compiler that you need to import all the classes present in the “util” package,which in turn present in the “java” package.The asterisk symbol tells the compiler to import all the classes in present in the util package.If you want to import specific class then we need to mention the name of the class instead of the asterisk.For eg: import java.util.Scanner
Built in packages:
1.java.lang:language supports classes.These are classes that java compiler itself uses and therefore automatically imported.They include classes for primitive types,stirngs,maths,threads and exceptions.
2.java.util:language utility classes such as vecotrs,hash tables,random numbers,date etc.
3.java.io:input/output support classes.They provide the facility for input/output of data.
4.java.awt:set of classes for implementing graphical user interface.They include classes for windows,buttons,lists
5.java.net:Classes for networking.They include classes for communicating with local computers as well as with internal servers.
Today we shall create a user defined package.For this we need to create a folder with your desired name.

package name_of_folder_given
public class A
 {
   public void display()
    {
      System.ou.println("Hello world");
     }
 }

Now we have created a package.We shall now reuse the above defined class in other program.

import name_of_the_folder_given.*;
class packagedemo
 {
   public static void main(String arg[])
    {
      A ob = new A();
      ob.display();
    }
 }

There fore we have reused the a class defined in other program.Before executing the second program we need to compile the package i.e. javac a.java
Output:
package_demo
you can download the code:
1.download package
2.download program

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , . Bookmark the permalink.

1 Response to Packages in Java with Examples…!!!

  1. Pingback: Programming with Packages…!!! | letusprogram....!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s