Thursday, September 13, 2012

PACKAGE

package is a collection of related classes.It helpsOrganize your classes into a folder structure and make it easy to locate and use them.
More importantly,It helps improve re-usability.
SYNTAX::
package ;
Example:
package Mypack;
//*** Mypack is name of folder in which your file should be saved.

Steps to create a package::

  • create a folder name MyPack
  • open your editor or notepad and type following class in MyPack package:
package MyPack;
public class Amount
{
                public double calamt(double price,double qty)
                {
                                    return(price*qty);
                 }
}
  • save above file in MyPack folder.
  • open a new notepad file and type following code to import above class Amount
import MyPack.Amount;
public class Bill
{
           public static void main(String args[])
           {
                    Amount ob = new Amount();   //object of class Amount
                    double amt = ob.calamt(150.5,2);
                   System.out.println("Please pay Rs. "+amt); 
           }
}

  • save above file outside MyPack folder. Compile and Run the file.





No comments: