Packages - The Most Powerful Feature of Java

Discussion in 'Java' started by manish174, Oct 16, 2010.

  1. manish174

    manish174 New Member

    Joined:
    Sep 17, 2010
    Messages:
    4
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    New Delhi
    Today we will discuss Java packages. This is one of the most important features in Java. Java comes with many pre-listed packages. Knowingly or unknowingly, you may have used them in your code. I will explain this later on. Even if you are a beginner you had used Java package. First of all we will learn what Java package is, then what its features, its importance are and how to use them in your program code.

    What is Java Package?



    The simplest example that will give you a most satisfactory explanation is, packages are like your book shelf. In your book shelf, you arrange school books in a row; in another row you arrange your comic books, your novels, your CD and many more. Similarly, Java also used several shelves to arrange similar kind of codes files that are used by programmers. These shelves are known as Java package. Also you must have heard about Java runtime environment ( jre ). When we compile a code it directly interact jre and use the default package which was set by Java. And the package is "java.lang". This default package is already imported in your program code. That’s why whenever we need to use the classes of java.lang package we don’t need to import this package. For other packages we need to import them explicitly.

    Java Package - Importance/features



    The one of the most important feature of Java package is that you can create your own package and import it in your program code without creating a .jar file. You simply need to set the classpath of your command line interpreter. For example, if you frequently use the code to calculate the area, then instead of writing the code to calculate the area in each program code you can simply write the code in a separate file and compile it. Then set the classpath of that particular class in your command line interpreter. Now you don’t need to write the same code again and again.

    How to use packages in your Program Code?



    Part A : Using Sun’s Java package.
    In this part we will learn how to use pre-existing package. Some of the most commonly used Java packages are

    java.awt
    java.applet
    java.io
    java.net
    java.sql
    javax.swing
    javax.sql
    java.awt.event
    and many more.

    Here I will show a simple code to use javax.swing package.

    In below program code we will use javax.swing package and will use some pre-existing code file.
    Code:
    import javax.swing  // it should be the  line to import any package in your program code.
    Class GuiApp
    {
    	JFrame f1;  // pre-existing code file named as JFrame javax.swing package
    	JPanel p1; // pre-existing code file named as JPanel javax.swing package
    	JLabel l1; // pre-existing code file named as JLabel in javax.swing package
    
    	GuiApp() // creating a default constructor of our class
    	{
    		f1 = new JFrame("GUI APPLICATION"); // using JFrame class by calling its constructor.
    		p1 = new JPanel();// using JPanel class by calling its default constructor.
    		l1 = new JLabel("MANISH RANA"); // using JLable class by calling its constructor.
    
    		f1.getContentPane().add(p1);
    		f1.setSize(300,300);
    		f1.setVisible(true);
    		p1.add(l1);
    	}
    	public static void main(String s[])
    	{
    		GuiApp g = new GuiApp();
    	}
    }
      
    Now in next example we will create our own package.
    Code:
    package Hello;
    class hel
    {
    	hel()
    	{
    		System.out.print("Hello Java");
    	}
    }
      
    Now compile this code as you normally compile.

    To compile this coed use the following command line code:

    Part 1: To compile in same directory
    javac –d .<filename>.java

    Run using following command

    java <package name>.<class file name>

    Note you need to remove the <filename> by your own file name. In above Example its hel.java and package name is Hello.

    Part 2: To other directory

    javac –d <path>/<filename>.java

    In this way you will be able to create your own package. Try some different types of code and use them in your code. And anytime you need help just drop a comment here.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. sbsl

    sbsl Banned

    Joined:
    Sep 20, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    good........thanks for sharing this, its good for beginners
     
  4. puja2sharma

    puja2sharma New Member

    Joined:
    Feb 13, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    packages is used as object. we create the different packages for different purpose.
     
  5. annasingh

    annasingh New Member

    Joined:
    Jan 10, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    thank you to given me great knowledge of packages
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice