473,385 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

How to use Packages In Java

sreekandank
Introduction:
Java allows you to group classes in collection called a package. Packages helps to separate your work from code libraries. The standard Java library contains a number of packages including java.lang, java.util, java.net, and so on.
The main reason for using package is to avoid conflict between classes. For example, suppose two programmers come up with the same Employee class. Then both of them place their class into different packages. So there is no conflict between classes.

Class Importation:
A class can use all classes from its own package and all public classes from other packages. You can access the public classes in another package in two ways:
  1. By adding the full package name in front of every class name.
    For example,
    Expand|Select|Wrap|Line Numbers
    1. class packageDemo
    2. {
    3.  public static void main(String args[])
    4.  {
    5.   java.util.Date today=new java.util.Date();
    6.   System.out.println("Todays Date : "+today);
    7.  }
    8. }
    9.  
  2. By placing import statements at the top of your source files.
    For example,
    Expand|Select|Wrap|Line Numbers
    1. import java.util.*;
    2. class packageDemo1
    3. {
    4.  public static void main(String args[])
    5.  {
    6.   Date today=new Date();
    7.   System.out.println("Today: "+today);
    8.  }
    9. }
Static Imports:
JDK5.0 or higher versions supports for importing static methods and fields from packages. For example,

import static java.lang.System.*;

Then you can use static methods and fields of the System class without the class name prefix. That is:

out.println("Welcome...");

Defining a Package:
To create a package, simply include a package command as the first statement in a Java source file. Any classes declared within that file belong to the specified package. When you create a package, the sub directory contains the package file must be sane as the package name. For example, the following statement creates a package called 'mypack'.

package mypack;

The java files that contains this mypack must be stored in a directory called 'mypack'.

Example:

Create the sub directory 'mypack' - contains the following three classes:
  1. Addition.java
    Expand|Select|Wrap|Line Numbers
    1. package mypack;
    2. public class Addition
    3. {
    4.  public double add(double a,double b)
    5.  {
    6.   return a+b;
    7.  }
    8. }
    9.  
  2. Multiplication.java
    Expand|Select|Wrap|Line Numbers
    1. package mypack;
    2. public class Multiplication
    3. {
    4.  public double mul(double a,double b)
    5.  {
    6.   return a*b;
    7.  }
    8. }
  3. Division.java
    Expand|Select|Wrap|Line Numbers
    1. package mypack;
    2. public class Division
    3. {
    4.  public double div(double a,double b)
    5.  {
    6.   return a/b;
    7.  }
    8. }

Create another java file named as 'packageDemo.java' that make the use of 'mypack' and its classes.

packageDemo.java
Expand|Select|Wrap|Line Numbers
  1. import mypack.*;
  2. /**
  3. *@author Sreekandan.K
  4. */
  5. class packageDemo
  6. {
  7.  public static void main(String args[])
  8.  {
  9.   Addition a=new Addition();
  10.   System.out.println("Addition:"+ a.add(5,3));
  11.   Multiplication m=new Multiplication();
  12.   System.out.println("Multiplication:"+m.mul(5,3));
  13.   Division d=new Division();
  14.   System.out.println("Division:"+ d.div(5,3));
  15.  }
  16. }
Note: For our convenient, place this(packageDemo.java) file into the bin directory of jdk1.3.1, that is:C:\jdk1.3.1\bin. To make the execution successfully open the attached output window below.

Author : SREEKANDAN.K
Attached Images
File Type: jpg output.jpg (69.4 KB, 407 views)
Oct 29 '11 #1
0 4389

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Lem | last post by:
I get the error Exception in thread "main" java.lang.NoClassDefFoundError when I type java app2 in the command prompt. I've tried moving to the jre directory and typed java c:\app2\app2, but it...
1
by: Mako Klaic | last post by:
what is java package? (for exaple javax.servlet) where are located package files? what file type is a package? im trying to include a package javax.servlet.http i have intalled both J2SE and...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
1
by: Robert Maas, see http://tinyurl.com/uh3t | last post by:
According to an online manual: http://www.croczilla.com/~alex/reference/javascript_ref/packages.html#1193137 all the Java API packages in java.* and sun.* are available from JavaScript. There's...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
5
by: TZESENG | last post by:
DECEMBER 13, 2005 . Editions: N. America | Europe | Asia | Edition Preference News Analysis By Steve Hamm Source: http://www.businessweek.com/technology/content/dec2005/tc20051213_042973.htm...
7
by: dlarsson | last post by:
Okay folks, I thought I was doing something very, very simple, but I cannot seem to get this to work at all. Can anyone identify what I am doing wrong here-? _________________________________ ...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
3
by: Doug Robinson | last post by:
If I try to access another jar files default package I have no problem. When I try to access anythig else i.e. package com.mine.thing, I always get the not found message. What special thing do I...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.