Java package

SAIROSHNI

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:

java package
Type of package

  • In build packages
  • User defined packages.

System 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
User defined packages:

To create your own package.

 Ways to create user-defined packages:


  1. creating a package
  2. Importing a package.
CREATE 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.s
Year: 2nd year

 


Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send