0% found this document useful (0 votes)
6 views6 pages

? Java Packages

Uploaded by

Samsung A12
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
6 views6 pages

? Java Packages

Uploaded by

Samsung A12
Copyright
© © All Rights Reserved
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

📦 Java Packages – Full Beginner to Advanced

Explanation

✅ 1. What is a Package in Java?


A package is a way to organize related classes and interfaces together into one unit. Think of it
like a folder on your computer that contains all the Java files for a specific module or feature.

📁 For example, a package called math might contain classes like Calculator, Geometry, and
Algebra.

✅ 2. Why Do We Use Packages? (Purpose)


Java packages help you:

 Organize code better: You can group similar classes together.


 Avoid name conflicts: Two classes with the same name can exist in different packages.
 Reuse code easily: You can import and use a package in many projects.
 Hide internal classes: Use access modifiers to control visibility.
 Collaborate in teams: Developers can work on separate packages without code
collisions.

✅ 3. Types of Packages
There are two main types:

🔹 a) Built-in Packages

These come with Java, and you can import them anytime.

Examples:

 [Link] → Scanner, ArrayList, HashMap


 [Link] → FileReader, BufferedReader
 [Link] → String, Math, Object (this one is auto-imported)
🔹 b) User-defined Packages

These are packages you create yourself in your project.

package [Link];

public class Helper {


public static void help() {
[Link]("Helping...");
}
}

✅ 4. How to Create a Package


To create a package in Java:

 Use the package keyword at the top of the file.


 The folder name must match the package name.

🧾 Example:
// File: tools/[Link]
package tools;

public class Message {


public void show() {
[Link]("Hello from tools package!");
}
}

💡 Always keep the Java file in the same folder as the package name (tools/ in this case).

✅ 5. Using a Package in Another Class


To use a class from a package, you use the import keyword:

import [Link];

public class Test {


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

💡 Java will look for a folder named tools with a class Message inside it.
✅ 6. Compiling and Running Java with Packages
When you use packages, compile using:

javac -d . [Link] # creates folders based on package name

Run using the full class name:

java [Link]

-d . tells the compiler to create folders (directories) as needed based on the package structure.

✅ 7. Package Naming Conventions


Follow these rules for naming packages:

 Use all lowercase letters.


 Use reverse domain name for unique naming (e.g., [Link]).
 Use short, meaningful names.

Examples:

 [Link]
 [Link]

✅ 8. Importing Packages
You can import classes from other packages in two ways:

a) Import one class


import [Link];

b) Import all classes


import [Link].*; // imports all classes like Scanner, ArrayList, etc.

Note: Importing all classes using * does not include sub-packages.


✅ 9. Static Import
Java allows you to import static methods/fields directly using import static.

import static [Link].*;

public class Example {


public static void main(String[] args) {
[Link](sqrt(25)); // no need to write [Link]()
}
}

✅ 10. Sub-Packages
A sub-package is just a package inside another package.

package [Link];

In folder form:

myapp/
└── utils/
└── tools/
└── [Link]

💡 You must import sub-packages manually — Java does not auto-import them.

✅ 11. Access Control with Packages


Java uses access modifiers to control class/method visibility across packages:

Access Modifier Visible in Same Package Visible in Other Packages


public ✅ Yes ✅ Yes
protected ✅ Yes ✅ Yes (only in subclasses)
default (no modifier) ✅ Yes ❌ No
private ❌ No ❌ No

So, if a class or method has no modifier, it is only accessible within the same package.
✅ 12. Folder Structure Example
project/
├── app/
│ └── [Link] // package app;
├── utils/
│ └── [Link] // package utils;

To compile:

javac -d . utils/[Link]
javac -d . app/[Link]

To run:

java [Link]

✅ 13. Real-World Analogy


Imagine your computer folders:

C:/MyPrograms/
└── school/
└── [Link]
└── games/
└── [Link]

You can think of:

package school;
package games;

Just like folders organize your files, packages organize your Java classes.

✅ 14. Common Built-in Packages


Package Contains
[Link] String, Math, System, Object
[Link] Scanner, ArrayList, HashMap
[Link] FileReader, BufferedWriter
[Link] Sockets, URLs, HTTP clients
[Link] JDBC classes for databases
[Link] GUI elements like JButton, JFrame
✅ 15. Common Mistakes with Packages
❌ Forgetting to match folder name with package statement
❌ Using default access when public is needed
❌ Forgetting to import the class
❌ Assuming import xyz.* includes sub-packages (it does not)
❌ Not compiling with -d . option

✅ 16. Advantages of Packages


✅ Keep your project organized and clean
✅ Avoid naming conflicts
✅ Allows modular programming (divide project into modules)
✅ Makes reusability and maintenance easier
✅ Helps teams work on different parts of a large project

You might also like