0% found this document useful (0 votes)
14 views4 pages

Java Packages

Java packages are used to organize classes and avoid name conflicts, allowing for better code management. There are built-in packages provided by Java and user-defined packages created by developers. The package keyword is used to create a package, while the import statement allows the use of classes from other packages, following specific naming conventions and folder structures.
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)
14 views4 pages

Java Packages

Java packages are used to organize classes and avoid name conflicts, allowing for better code management. There are built-in packages provided by Java and user-defined packages created by developers. The package keyword is used to create a package, while the import statement allows the use of classes from other packages, following specific naming conventions and folder structures.
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
You are on page 1/ 4

Java Packages

Hello everyone! Today, I'm going to talk about Java Packages. Let's start with a simple definition.

What is a Java Package?


In Java, a package is like a folder on your computer. It helps us group related classes and code together.
By organizing classes into packages, we can keep our code neat and avoid confusion. For example, you
might have a package for all math-related classes, and another package for all classes that deal with
graphics.

We use packages to avoid name conflicts and make code easier to manage. Imagine having two classes
both named Helper in different parts of a big project. If they are in different packages, Java can tell them
apart by their package names, just like folder paths in your file system.

Why Use Packages?


There are several reasons we use packages in Java: - Organization: Packages help group related classes,
making it easier to find and manage code. For example, all utility classes might go in a package called
utilities . - Avoiding Name Conflicts: With packages, you can have two classes with the same name in
different packages (for example, com.app.ui.Button and com.app.backend.Button ) and they won’t
clash. - Access Control: Packages can control who can see classes and methods. Classes in the same
package can access each other’s features more easily, while others from outside packages may not see
internal details. - Reusability: By keeping classes in packages, it’s easier to reuse code in different projects
by importing the packages. - Teamwork: Packages let different people work on different parts of a program
without interfering. For example, one person can work on classes in com.app.ui while another works on
com.app.backend .

Types of Packages: Built-in and User-Defined


Java has two main types of packages:

• Built-in Packages: These are packages that come with Java itself. They are part of the Java API.
Examples include:
• java.lang : Contains core classes like String , Math , System . (This package is automatically
imported, so you don’t have to write import java.lang.String to use String .)
• java.util : Contains utility classes like ArrayList , Scanner , Date , and more.

• java.io : Contains classes for input and output (like File , InputStream , etc.). These packages
are already there for us to use anytime.

1
• User-Defined Packages: These are packages we create ourselves when writing our own code. We
do this to organize our program. For example, if you are writing a game, you might create packages
like com.game.characters and com.game.levels to hold different classes. Another simple
example might be a package named myapp.utilities where you put helper classes for your
project.

Examples of Java Packages


• A common built-in package is java.util . For example, if you want to read user input, you often
use java.util.Scanner .
• Another built-in example is java.io , which has classes for reading and writing files.
• A user-defined example could be com.school.project if you're working on a school project
named "project".
• Or org.example.mypackage if you follow the convention of using your (reversed) domain name
as a prefix.

Creating and Using a Package


To create a package in Java, you use the package keyword at the top of your Java file. The package name
usually matches the folder structure where the file is located.

For example, suppose we want to create a package called com.example.utils and put a class
Greeter inside it. The code might look like this:

// File: com/example/utils/Greeter.java
package com.example.utils;

public class Greeter {


public static void sayHello() {
System.out.println("Hello from Greeter!");
}
}

This means Greeter.java is in the folder com/example/utils and it belongs to the package
com.example.utils . The package line tells Java which package the class is in.

Now, to use the Greeter class from another part of our program, we can use the import statement
(more on that next). For example:

// File: com/example/Main.java
package com.example;

import com.example.utils.Greeter;

2
public class Main {
public static void main(String[] args) {
Greeter.sayHello(); // Calls the method from the Greeter class
}
}

In this example, Main.java is in com/example and declares package com.example; . It uses


import com.example.utils.Greeter; so it can directly use Greeter .

The import Statement


The import statement in Java is how we tell the compiler we want to use classes from another package.
There are two common ways to import: - Import a specific class:
import com.example.utils.Greeter; - Import an entire package:
import com.example.utils.*; (the * means all classes in that package)

After using an import, we can refer to the class by its simple name ( Greeter instead of
com.example.utils.Greeter ). If we don't import, we'd have to write the full package name every time
we use the class.

For example, instead of Greeter.sayHello() , without import we would have to write


com.example.utils.Greeter.sayHello() . So using import makes our code cleaner.

Naming Conventions and Folder Structure


When naming packages in Java, there are some common conventions: - All lowercase: Package names are
usually all lower-case letters, like com.example.app or myapp.utils . - Domain name reverse
(optional): Many organizations start with a reversed domain name (e.g., com.company.project ). This
helps ensure uniqueness if multiple companies or projects are involved. - Folder structure: Each part of the
package name corresponds to a folder. For example, com.example.app means the files are in com/
example/app/ folders. So if your class is in package com.example.app; then the file path should be
com/example/app/ClassName.java . - Default package: If you don't declare a package, Java uses a
default (unnamed) package. This is fine for small examples, but in larger projects it's best to always define a
package. - Single directory: If you use a simple name like package myapp; , then put the Java files in a
folder named myapp .

Always make sure the folder structure matches the package name exactly, or Java won't find your classes.

Summary
In summary, Java packages are used to organize Java classes and avoid name conflicts. We have built-in
packages (provided by Java) and user-defined packages (created by us). Packages help keep code clean
and grouped by purpose. We use the package keyword to create a package and the import statement
to use classes from other packages. Follow naming conventions and the correct folder structure so Java can
locate your classes.

3
For example, in large projects it becomes much easier to find and use classes when they are neatly
organized in packages. Using packages wisely keeps your code organized and makes your life as a
programmer much simpler.

That's all for today. Thank you!

You might also like