Login    New User    Forgot Password    Help      

Java Package

Java Tutorials

Many times when we get a chance to work on a small project, one thing we intend to do is to put all java files into one single directory. It is quick, easy and harmless. However if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would be a nightmare for us. In java we can avoid this sort of problem by using Packages.

How to create a package

Suppose we have a file called HelloWorld.java, and we want to put this file in a package world. First thing we have to do is to specify the keywordpackage with the name of the package we want to use (world in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our HelloWorld class below:

// only comment can be here
package world;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

 

 

Package

// Package
/*
Combination of 4 pack of java program
b.java           PROGRAM NO 33
line.java        PROGRAM NO 34
table.java      PROGRAM NO 35
e.java           PROGRAM NO 36
Steps to create package
1. First create above 5 files
2. Create calpack directory
3. Copy all class files of above 5 files in that directory
4. Now cal this file
import calpack.b;// calling b class for calpack
import calpack.* ; // calling all class files from calpack
cal this as java cal 5

SUMMARY OF PACKAGE

CLASS NAME             METHODS                  DISCRIPTION

B.CLASS                 B                           SHOW PACK IS OPEN
TABLE                     TABLE                   FOR TABLE PROGRAM
LINE                       L                           FOR LINE
E                           E                           END OF PACKAGE

*/

Example

import calpack.*;

class cal
{
public static void main(String args[])
{
int n;
n=Integer.parseInt(args[0]);
// line is class and l is function
line obline=new line();

obline.l();
// b is class declated in pack and packopen is function of b class
b ob2= new b();
ob2.b();
obline.l();

table ob3=new table();
ob3.tablet(n);

obline.l();
// e is class declated in pack and packopen is function of e class
e obe= new e();
obe.e();
obline.l();

 

}
}

 

Output

Program to display Pack is open

// Save and compile this file as b.java in calpack folder
// Creating a package name calpack
package calpack;

//defining a public class

public class b
{
int i;
        public void b()
        {
               
        System.out.println("Pack is open ");
        
        }
}

Program to display line

// Save and compile in calpack folder as line.java
// Creating a package name calpack
package calpack;

//defining a public class

public class line
{
public int i;
        public void l()
        {
        for(i=0;i<=10;i++)
                {
                System.out.print("* *");
                }
        System.out.println();
        }
}

Program to display table

// save and compile in calpack folder as table.java
package calpack;
public class table
{
public int i;
        public void tablet(int m)
	{
	for(i=1;i<=10;i++)
		{
		System.out.println(m +"\t*\t" + i+"\t=\t" + m*i);
		}
	}
}

Program to display Pack is close


// save and compile in calpack folder as e.java
// creating a package name calpack
package calpack;

//defining a public class

public class e
{
int i;
        public void e()
        {
               
        System.out.println("Pack is Close ");
        
        }
}

Java Tutorials