Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, a package is a collection of related classes and interfaces organized in a namespace. Packages help to avoid naming conflicts, provide better organization of code, and improve code reusability. In this tutorial, we will cover the basics of Java packages, including creating packages, importing packages, and using package access modifiers.
To create a package, you need to add a package declaration as the first non-comment line in your Java source file. The package declaration follows the syntax package packageName;
. The package name should be in lowercase and follow the reverse domain name convention.
Example:
// File: com/example/animals/Animal.java package com.example.animals; public class Animal { // Class implementation }
When you want to use a class or interface from another package, you need to import it using the import
statement. The import
statement should be placed after the package declaration and before the class or interface definition.
There are two ways to import classes or interfaces from a package:
import packageName.ClassName;
.import com.example.animals.Animal; public class Main { public static void main(String[] args) { Animal animal = new Animal(); // ... } }
import packageName.*;
.import com.example.animals.*; public class Main { public static void main(String[] args) { Animal animal = new Animal(); // ... } }
Java provides four access modifiers to control the visibility of class members (variables, methods, and inner classes): public
, private
, protected
, and package-private (default). Package-private members are accessible only within the same package and do not require any explicit access modifier.
Example:
// File: com/example/animals/Animal.java package com.example.animals; class Animal { // Class implementation } // File: com/example/animals/Dog.java package com.example.animals; public class Dog extends Animal { // Class implementation }
In the example above, the Animal
class has a package-private access modifier, meaning it is only accessible within the com.example.animals
package. The Dog
class, which is in the same package, can access and extend the Animal
class.
In this tutorial, we discussed the basics of Java packages, including creating packages, importing packages, and using package access modifiers. Packages are an essential part of organizing your code, avoiding naming conflicts, and improving code reusability in your Java applications.
Creating and Using Packages in Java:
package
statement at the top of your Java source file.// File: MyPackageClass.java package com.example.mypackage; public class MyPackageClass { // Class implementation }
Java Package Naming Conventions:
package com.example.util;
Import Statement in Java for Packages:
import
statement is used to bring classes from other packages into the current source file.import com.example.mypackage.MyPackageClass; public class AnotherClass { MyPackageClass myObj = new MyPackageClass(); }
Java Package Structure and Organization:
src/ ������ com ������ example ������ mypackage ������ MyPackageClass.java
How to Organize Classes in Java Packages:
src/ ������ com ������ example ������ util ������ StringUtil.java ������ NumberUtil.java
Java Package Visibility and Access Modifiers:
public
, protected
, private
) control visibility outside the package.// In StringUtil.java package com.example.util; public class StringUtil { // Class implementation }
Java Package-Private (Default) Access:
// In StringUtil.java package com.example.util; class StringUtil { // Package-private class }
Java Package Hierarchy and Directory Structure:
src
directory.src/ ������ com ������ example ������ util ������ StringUtil.java
Java Packages and Classpath:
javac -d . MyPackageClass.java java com.example.mypackage.MyPackageClass