This document discusses packages in Java. Packages are used to group related classes and avoid name conflicts. There are two types of packages: built-in packages from the Java API library that contain prewritten classes, and user-defined packages that allow creating your own packages by using the package keyword and saving files in a corresponding directory. The code shows an example of creating a user-defined package called mypack that contains a MyPackageClass with a main method to call an add method and output results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
67 views2 pages
Experiment 5 Aim: Theory
This document discusses packages in Java. Packages are used to group related classes and avoid name conflicts. There are two types of packages: built-in packages from the Java API library that contain prewritten classes, and user-defined packages that allow creating your own packages by using the package keyword and saving files in a corresponding directory. The code shows an example of creating a user-defined package called mypack that contains a MyPackageClass with a main method to call an add method and output results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
EXPERIMENT 5
AIM: Program on Packages
THEORY:
A package in Java is used to group related classes. Think of it as a folder in a file
directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories:
1. Built-in Packages (packages from the Java API)
The Java API is a library of prewritten classes, that are free to use, included in the Java Development Environment. The library contains components for managing input, database programming, and much more. The library is divided into packages and classes. Meaning you can either import a single class (along with its methods and attributes), or a whole package that contain all the classes that belong to the specified package. To use a class or a package from the library, you need to use the import keyword.
2. User-defined Packages (create your own packages)
To create your own package, you need to understand that Java uses a file system directory to store them. To create a package, use the package keyword. Save the file as MyPackageClass.java, and compile it. When we compiled the package in the example above, a new folder was created, called "mypack". To run the MyPackageClass.java file
CODE: package mypack; class MyPackageClass { void add(int a, int b, int c){ int sum = a+b+c; System.out.println("The sum is "+sum); }
public static void main(String[] args) {
MyPackageClass p1 = new MyPackageClass(); System.out.println("This is my package!"); p1.add(69,420,46);