PACKAGE IN JAVA
A Java package is a collection of sub-packages, interfaces, and classes of a common type.
Many pre-built packages are available, including java, lang, awt, javax, swing, net, io, util, sql, etc.
Types of packages:
- User defined packages
- Build-in packages
Syntax:
Package packagename;
e.g.: package pack;
Example:
package pack;
public class demo
{
public static void main(String args[])
{
System.out.println("Welcome to package");
}
}
1. System packages: (just import and use)
They are in-built in the system.
Ex:
java.lang → language package
java.util→ utility package
java.io→Input /output package
java.awt→ GUI package
java.net→ Networking package
java.applet → Applet package
java.sql → Database package
2. User defined package:
Steps:
1. Create packages
2. Import packages
1.Create Packages:
1. Package package_name;
Ex:
Package College;
Keyword PackageName
2. Declare class as public
Ex: public class classname
3. Save & compile the file
2. Importing Packages:
Import packagename.*;
Keyword
Ex: import college.*;
Example:1
package program
package College;
public class Course
{
String dept;
String year;
Course(String d, String y)
{
dept=d;
year=y;
}
public void display()
{
System.out.println (“Dept: “ + dept);
System.out.println (“Year: ” + year);
}
import college.*;
class packdemo
{
public static void main(String as[])
{
Course C = new Course(“C.s”, “2nd year”);
C.display();
}
}
Example:2
package college;
public class simple
{
public Void message()
{
System.out.println("welcome to my collegee");
}
}
import college.*;
public class A
{
public static void main(String args[])
}
{
College obj=new college[];
Obj.message();
}
}