0% found this document useful (0 votes)
3 views3 pages

Java Packages: Custom & Built-in Guide

This guide explains Java packages, including their definition, advantages, and rules for creating custom packages. It provides step-by-step instructions for creating and using custom packages, as well as examples of nested packages and a brief overview of commonly used built-in packages in Java. Additionally, it offers tips for effective package management and organization.

Uploaded by

Tanmay. Jare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Packages: Custom & Built-in Guide

This guide explains Java packages, including their definition, advantages, and rules for creating custom packages. It provides step-by-step instructions for creating and using custom packages, as well as examples of nested packages and a brief overview of commonly used built-in packages in Java. Additionally, it offers tips for effective package management and organization.

Uploaded by

Tanmay. Jare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Custom Packages and Built-in Packages Guide

1. What is a Package?
A package is a namespace that organizes classes, interfaces, and sub-packages. - Helps avoid naming
conflicts. - Makes code modular and reusable. - Java has built-in packages like [Link] , [Link] . -
Custom packages are user-defined.

2. Advantages of Packages
1. Modularity: Groups related classes.
2. Reusability: Classes in a package can be reused in multiple programs.
3. Namespace management: Prevents naming conflicts.
4. Access protection: Provides controlled access using public or default classes.
5. Easy maintenance: Structured organization of code.

3. Rules for Creating Packages


1. Package name should be all lowercase.
2. The folder structure must match the package name.
3. Always place the package statement at the top of the file.
4. Classes in a package can be public (accessible outside) or default (accessible only within the
package).
5. For nested packages, the folder structure should match the hierarchy.

4. How to Create a Custom Package


Step 1: Create a package

// File: MyPackage/[Link]
package MyPackage;

public class Hello {


public void showMessage() {
[Link]("Hello from custom package!");

1
}
}

- Folder structure: MyPackage/[Link]

Step 2: Compile the package

javac MyPackage/[Link]

Step 3: Use the package in another class

// File: [Link]
import [Link];

public class TestPackage {


public static void main(String[] args) {
Hello obj = new Hello();
[Link]();
}
}

Step 4: Compile and run

javac [Link]
java TestPackage

Output:

Hello from custom package!

5. Nested Packages Example

package [Link];

public class Utility {


public void printMessage() {
[Link]("Message from nested package");

2
}
}

- Folder structure: mycompany/utils/[Link]

6. Built-in Packages in Java


Here’s a brief overview of commonly used built-in packages:

1. [Link]: Automatically imported; contains fundamental classes like String , Math , Integer ,
Object .
2. [Link]: Utility classes like ArrayList , HashMap , Date , Collections .
3. [Link]: For input and output; classes like File , BufferedReader , FileWriter .
4. [Link]: Classes for networking like Socket , URL , HttpURLConnection .
5. [Link]: JDBC API; classes like Connection , Statement , ResultSet .
6. [Link]: GUI components; classes like JFrame , JButton , JLabel .
7. [Link]: Date and time API; classes like LocalDate , LocalTime , LocalDateTime .
8. [Link]: Classes for security like MessageDigest , SecureRandom .
9. [Link]: Classes for multithreading and concurrency like ExecutorService ,
ConcurrentHashMap .

7. Tips
• Keep package declaration at the top.
• Use meaningful names for packages like banking , utilities , io .
• You can create jar files from packages for distribution.

This guide now covers custom packages, creation rules, examples, sample outputs, nested packages,
and a brief overview of commonly used built-in packages in Java.

You might also like