06.Packages & Modifiers
Perfect! Let’s move on to Packages in Java — a fundamental concept to organize classes and interfaces. I’ll explain it in full detail.
Packages in Java
1. What is a Package?
Definition: A package is a collection of related classes, interfaces, and sub-packages.
Purpose:
Organize code into logical groups.
Avoid naming conflicts (classes with same names).
Control access using access modifiers (
public,protected, etc.).Reusable and easier to maintain.
2. Types of Packages
Built-in / Predefined Packages: Provided by Java SDK. Examples:
java.lang– automatically imported, contains fundamental classes (String,Math,Object).java.util– collection framework,Date,Random.java.io– file handling classes (File,BufferedReader).java.sql– database connectivity.
User-defined Packages: Created by developers to organize their own classes.
Unnamed / Default Package: Classes written without package declaration automatically go here. Usually for small programs.
3. How to Create a User-defined Package
Step 1: Declare a package
// File: Car.java
package vehicles; // package declaration
public class Car {
public void display() {
System.out.println("This is a car");
}
}
Step 2: Compile the package
javac -d . Car.java
-d .tells compiler to create folder structure for the package (vehiclesfolder).
Step 3: Use the package in another class
// File: Main.java
import vehicles.Car; // import package
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.display();
}
}
4. Access Modifiers in Packages
| Modifier | Visibility / Scope |
public | Visible everywhere |
protected | Visible in package + subclasses |
| default (no modifier) | Only visible in the same package |
private | Only visible inside the class |
5. Advantages of Packages
Code Reusability – Organize classes to reuse them in different projects.
Better Maintainability – Easier to manage large projects.
Namespace Management – Avoids class name conflicts.
Access Control – Control which classes/members are visible outside the package.
6. Built-in Package Example
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list);
}
}
✅ Summary Table – Package Types
| Package Type | Description | Examples |
| Built-in / Predefined | Provided by Java SDK | java.lang, java.util, java.io |
| User-defined | Created by programmer | package vehicles; |
| Unnamed / Default | No package declared | Small programs, testing |
Perfect! Let’s cover Non-Access Modifiers in Java in full detail. These are modifiers that do not control access, but instead change behavior or properties of classes, methods, or variables.
Non-Access Modifiers in Java
1. What is a Non-Access Modifier?
Definition: Modifiers that modify behavior of classes, methods, or variables without affecting access control.
Used for:
class,method,variable
2. Types of Non-Access Modifiers
| Modifier | Can be applied to | Purpose / Behavior | Example |
| final | Class, method, variable | - Class: Cannot be extended |
- Method: Cannot be overridden
- Variable: Constant value | final int x = 10;final class Car {}final void start(){} |
| static | Method, variable, block, nested class | Belongs to class, shared by all objects | static int count = 0;static void display(){} |
| abstract | Class, method | - Class: Cannot create objects
- Method: Must be implemented by subclass | abstract class Vehicle { abstract void start(); } |
| synchronized | Method, block | Thread-safe; only one thread can execute at a time | synchronized void increment() {} |
| native | Method | Method implemented in other language (C/C++) | native void display(); |
| transient | Variable | Not serialized during object serialization | transient String password; |
| volatile | Variable | Ensures visibility of changes across threads | volatile int counter; |
| strictfp | Class, method | Forces floating-point calculations to be platform-independent | strictfp class Calculator {} |
3. Detailed Examples
final
final class Car {} // cannot be extended
final int speed = 100; // constant
final void start() { System.out.println("Start"); } // cannot override
static
class Car {
static int wheels = 4; // shared by all objects
static void display() { System.out.println("Static method"); }
}
abstract
abstract class Vehicle {
abstract void start(); // must be implemented in subclass
}
class Bike extends Vehicle {
void start() { System.out.println("Bike started"); }
}
synchronized
class Counter {
private int count = 0;
synchronized void increment() { count++; } // thread-safe
}
native
class Example {
native void display(); // implemented in C/C++
}
transient
class User implements java.io.Serializable {
transient String password; // not saved during serialization
}
volatile
class Counter {
volatile int count; // ensures thread visibility
}
strictfp
strictfp class Calculator {
float divide(float a, float b) { return a / b; }
}
4. Quick Notes
final, static, abstract are the most commonly used.
transient, volatile, synchronized, native, strictfp are mostly advanced and used in multithreading, serialization, and system-level programming.
Cannot combine some modifiers:
abstractandfinalcannot be used together.abstractandprivatecannot be used together.
✅ Summary Table
| Non-Access Modifier | Applicable To | Purpose |
| final | Class, Method, Variable | Constant, prevent inheritance/overriding |
| static | Method, Variable, Block, Class | Shared by class, not object |
| abstract | Class, Method | Partial implementation, force subclass implementation |
| synchronized | Method, Block | Thread-safe execution |
| native | Method | Implemented in other language |
| transient | Variable | Skip serialization |
| volatile | Variable | Thread visibility |
| strictfp | Class, Method | Platform-independent floating-point |