Youtube Channel

Package

Definition
  • A package is a collection of classes ,interfaces and sub packages. A sub packages is interns contains classes,interfaces and sub sub packages.
Purpose
  • The purpose of package is providing the clarity b/w different classes an interfaces which are present in java applications.
Advantage
  • The advantage of package concept is to providing conman classes and interfaces to the team of people in software development . In other words as a team leader if we want to give a class or interface to most of the java programmers such type of classes and interfaces must be placed in packages.
How to create a package in java
lets create a package which will have logic to take student details and this class will be imported in another class after package creation.
//Student.java
package myprog;//package name
public class Student
{
String name;
int id;
float per;
public void printStudent(String name,int id,float per)
{
this.name=name;
this.id=id;
this.per=per;
System.out.println(“name of student is”+name);
System.out.println(“roll no of student is “+id);
System.out.println(“percentage of student is “+per);
}
}//Student class Ends
How to compile this program
Syntax:
c:\javac [space]-d [space].[space]program.java
e.g
c:\javac -d . Student.java
Note:
As indicated in first line of program package mypack will be created in the folder currently you are contains the .class file of Student.java.
How to use the package created 
import myprog.Student;//importing the Student class i.e myprog.Student
class StudentDemo
{
public static void main(String[] args)
{
Student so=new Student();
so.printStudent(“kamlesh”,101,82f);
}
}//End
Who ever is working on the current machine where above package is available those people can make use of Student.class but other java programmers are unable to use student.class.
  • In software industry if any of the packages want to be used by every java programmer .
  • The team leader or project leader will generate a jar file . the process of combining all the created packages in the local machine in single compressed file and supping to the external world is JARING.
How to create jar files
SYNTAX
C:\JAR CFV FILENAME.JAR PACKAGENAME
EXAMPLE
c:\jar cfv mystudent.jar mypack
  • Now the mystudent.jar file will be created in the same folder or current working folder.
  • In real world we can keep our jar file in the net and other s/w engineers can download and make it available in their local system.
  • If other s/w engineer wnat to use or verify classes and interfaces present in the jar file then they need to set class path
  • set classpath=c:\java\fun\mystudent.jar
  • You can view all classes details using c:\javap mypack.Student
  • In real world most of the third party s/w vendors are giving lot of classes and interfaces to the java programmers in the form of packages and releasing on the name of jar files

0 comments: