What is a Package?
A package is a namespace that organises a set of related classes and interfaces โ similar to a folder on your file system. Packages prevent naming conflicts and provide access control.
Benefits
- Avoids naming conflicts (two classes can have same name in different packages)
- Provides access protection
- Makes code easier to locate and maintain
Types of Packages
| Type | Description | Examples |
|---|---|---|
| Built-in | Provided by Java SDK | java.lang, java.util, java.io |
| User-defined | Created by the programmer | com.college, in.anits.cse |
Creating Packages
The package statement must be the first line in a Java source file (before any imports or class declarations).
// File: in/anits/cse/Student.java
package in.anits.cse;
public class Student {
private int rollNo;
private String name;
public Student(int r, String n) { rollNo = r; name = n; }
public void show() {
System.out.println("Roll: " + rollNo + " | Name: " + name);
}
}# Compile with package structure
javac -d . in/anits/cse/Student.java
# This creates: ./in/anits/cse/Student.class
# Run:
java in.anits.cse.StudentImporting Packages
| Import style | Example | What it imports |
|---|---|---|
| Fully qualified name | java.util.Scanner sc = ... | Single class, no import needed |
| Single type import | import java.util.Scanner; | Only Scanner class |
| On-demand import | import java.util.*; | All classes in java.util (NOT sub-packages) |
| Static import | import static java.lang.Math.*; | All static members of Math |
import java.util.Scanner;
import java.util.ArrayList;
import in.anits.cse.Student;
public class Main {
public static void main(String[] args) {
Student s = new Student(101, "Arjun");
s.show();
}
}โ ๏ธ Important'import java.util.*' does NOT import sub-packages. For example, it does not import java.util.concurrent.* โ you must import sub-packages separately.
Access Modifiers
| Modifier | Same Class | Same Package | Subclass | Other Package |
|---|---|---|---|---|
| private | โ | โ | โ | โ |
| (default) | โ | โ | โ | โ |
| protected | โ | โ | โ | โ |
| public | โ | โ | โ | โ |
๐ Key PointUse
private for data members (encapsulation), public for API methods, and protected for methods intended to be overridden by subclasses.Built-in Packages
| Package | Contents |
|---|---|
| java.lang | Auto-imported; String, Math, System, Integer, Thread, Object... |
| java.util | Scanner, ArrayList, HashMap, Collections, Date... |
| java.io | File, FileReader, BufferedReader, FileWriter, InputStream... |
| java.net | Socket, URL, HttpURLConnection, InetAddress... |
| java.sql | Connection, DriverManager, ResultSet, PreparedStatement... |
| java.awt | GUI components: Button, Frame, Panel, Graphics... |
| javax.swing | Modern GUI: JFrame, JButton, JLabel, JTable... |
Static Import
Static import allows you to use static members of a class directly without the class prefix.
import static java.lang.Math.*;
import static java.lang.System.out;
public class MathDemo {
public static void main(String[] args) {
// Without static import: Math.sqrt(16), System.out.println
out.println(sqrt(16)); // 4.0
out.println(pow(2, 10)); // 1024.0
out.println(PI); // 3.141592653589793
}
}
Download PDF Notes
Save these notes as a PDF for offline reference