1. Introduction
A package in Java is a namespace mechanism used to group related classes, interfaces, and sub-packages together, helping avoid naming conflicts and organize large codebases logically.
Cricket analogy: A package is like grouping players into franchises—Mumbai Indians, Chennai Super Kings—so two players named 'Sharma' on different teams never get confused, keeping the IPL organized just as Java packages group related classes to avoid naming conflicts.
Java itself ships thousands of classes organized into packages such as java.util, java.io, and java.lang, and every project typically defines its own packages mirroring its folder structure.
Cricket analogy: Just as the ICC organizes formats into distinct categories—Test, ODI, T20—each with its own rulebook, Java ships thousands of built-in classes into packages like java.util, java.io, and java.lang, and projects mirror that same organized folder structure.
2. Syntax
// File: com/example/utils/MathHelper.java
package com.example.utils;
public class MathHelper {
public static int square(int n) {
return n * n;
}
}
// File: com/example/app/Main.java
package com.example.app;
import com.example.utils.MathHelper;
public class Main {
public static void main(String[] args) {
System.out.println(MathHelper.square(5));
}
}3. Explanation
The package declaration, if present, must be the very first non-comment line in a Java source file — before any import statements or class declarations. It tells the compiler which namespace the class belongs to and dictates the required directory structure (e.g., com.example.utils maps to the folder com/example/utils/).
Cricket analogy: The package declaration is like announcing the toss result before anything else happens in a match—it must come first, before team lineups (imports) or play (class code), and it dictates the ground's layout just as com.example.utils maps to folder com/example/utils/.
To use a class from another package, you either add an import statement at the top of the file, or refer to the class by its fully qualified name (e.g., com.example.utils.MathHelper.square(5)) directly in the code without importing it at all.
Cricket analogy: To bring in a foreign player, a franchise either signs them permanently onto the roster (import) or brings them in as a one-match guest referred to by full name and country (fully qualified name like com.example.utils.MathHelper.square(5)).
When no access modifier is specified for a class member, it has package-private (default) access, meaning it is visible only to other classes within the same package — not to subclasses or other code in different packages.
Cricket analogy: Package-private access is like a team's internal training drills visible only to players on that same franchise's home ground, not even to a subclass player traded to another team, unless the access is explicitly opened up.
Exam tip: import statements only save you from typing the fully qualified name; they do not affect runtime behavior or performance. Using the fully qualified name everywhere compiles and runs identically to using import.
A source file can have at most one package declaration and it must come first (only comments may precede it). Multiple package statements or placing it after an import statement is a compile-time error.
4. Example
package com.example.demo;
class Helper {
// package-private method: visible only within com.example.demo
static void greet() {
System.out.println("Hello from Helper");
}
}
public class Main {
public static void main(String[] args) {
Helper.greet();
// fully qualified name usage, no import needed since same file/package
System.out.println("Running in package: " + Main.class.getPackageName());
}
}5. Output
Hello from Helper
Running in package: com.example.demo6. Key Takeaways
- Packages are a namespace mechanism to organize related classes and avoid naming collisions.
- The package declaration must be the first line in a source file (comments aside).
- import lets you use a short class name instead of the fully qualified name.
- You can always use a class's fully qualified name as an alternative to importing it.
- Package-private (default) access restricts visibility to classes within the same package.
Practice what you learned
1. Where must the package declaration appear in a Java source file?
2. What is the purpose of the import statement in Java?
3. What access level does a class member have when no access modifier is specified?
4. Can you use a class from another package without importing it?
5. How many package declarations can a single Java source file have?
Was this page helpful?
You May Also Like
Interfaces in Java
Learn how Java interfaces define a contract of behavior, support multiple inheritance of type, and how default/static methods work since Java 8.
Encapsulation in Java
Learn how encapsulation in Java bundles data and behavior together using private fields, access modifiers, and getters/setters.
static Keyword in Java
Learn how the static keyword creates class-level fields, methods, and blocks shared across all objects in Java.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics