PACKAGES
- Packages is a collection of different types of java files.
- Packages is a group of class and interfaces.
- They're in built.
- It is similar to a ''folder''.
- To use a class or a package from the library, you need to use the ''import'' keyword.
THERE ARE TWO TYPES OF PACKAGES:
Type of package |
- In build packages
- User defined packages.
They are in-built in the system.
- 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
To create your own package.
Ways to create user-defined packages:
- creating a package
- Importing a package.
- package package_name;
EX: package college; 👇 👇
Keyword Package name- Declare class as public
EX: public class classname.- Save & compile the file
IMPORTING PACKAGES:
Import packagename.*; 👇
Keyword
EX: import college.*; 👇
This statement with import all the classes and interfaces in college package.
EXAMPLE:
//package program
package college;public class course {string dept;string year;course(String d, String y){}
//importing the package
import college.*;class packdemo{public static void main(String as[]){course C=new course(''C.s'',''2nd year'');C.display();dept=d;year=y;}public void display(){system.out.println(''Dept:''+dept);system.out.println(''Year:''+year);}}}
OUTPUT:Dept: C.sYear: 2nd year
- Declare class as public
- Save & compile the file
Keyword