UNIT-3
✅ 1. Nested Classes
🔹 What are Nested Classes?
Nested classes are classes defined within another class.
🔹 Types of Nested Classes in Java:
pgsql
CopyEdit
OuterClass
├── Static Nested Class
└── Inner Class
├── Member Inner Class
├── Local Inner Class
└── Anonymous Inner Class
🔹 Diagram:
pgsql
CopyEdit
+--------------------+
| OuterClass |
| |
| +----------------+ |
| | StaticNested | | <--- like a static member
| +----------------+ |
| |
| +----------------+ |
| | MemberInner | | <--- accesses outer members
| +----------------+ |
| |
| method() { |
| class Local {} | <--- defined in method
|} |
+--------------------+
🔹 Example:
java
CopyEdit
class Outer {
int x = 10;
class Inner {
void show() {
System.out.println("x = " + x); // access outer class
variable
}
}
static class StaticInner {
void display() {
System.out.println("Inside static inner class");
}
}
}
✅ 2. Exception Handling
🔹 Key Concepts:
Exception: Runtime error (e.g., divide by zero, file not
found).
Try-Catch-Finally: Used to handle exceptions.
Throw: Used to explicitly throw an exception.
Throws: Declares an exception might occur.
🔹 Flowchart:
vbnet
CopyEdit
START
|
try block
|
[Exception?]
| \
Yes No
| |
catch block Continue
|
finally block
|
END
🔹 Example:
java
CopyEdit
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("This always runs.");
}
✅ 3. Exception Handlers
🔹 Common Handlers:
ArithmeticException
ArrayIndexOutOfBoundsException
NullPointerException
IOException
🔹 Example:
java
CopyEdit
try {
int[] arr = new int[3];
arr[5] = 100; // throws exception
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds");
}
✅ 4. Concurrent Programming
Concurrent programming allows multiple tasks (threads) to
run simultaneously.
🔹 Benefits:
Better CPU usage
Faster performance in I/O-heavy programs
✅ 5. The Thread Class and Runnable Interface
🔹 Creating a Thread (2 Ways):
Method 1: Extending Thread class
java
CopyEdit
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
Method 2: Implementing Runnable interface
java
CopyEdit
class MyRunnable implements Runnable {
public void run() {
System.out.println("Runnable running");
}
}
🔹 Flowchart:
sql
CopyEdit
Create Thread
|
Start Thread
|
run() method executes
|
Thread terminates
✅ 6. Thread Priorities
🔹 Java Thread Priorities:
Thread.MIN_PRIORITY = 1
Thread.NORM_PRIORITY = 5 (default)
Thread.MAX_PRIORITY = 10
java
CopyEdit
Thread t1 = new Thread();
t1.setPriority(Thread.MAX_PRIORITY);
✅ 7. Synchronization
🔹 Problem: Race Conditions
Two threads accessing shared data can corrupt it.
🔹 Solution: synchronized block/method
java
CopyEdit
synchronized(obj) {
// critical section
}
✅ 8. Java’s I/O Streams
🔹 Types of Streams:
Byte Streams: InputStream, OutputStream
Character Streams: Reader, Writer
✅ 9. Byte Streams and Character Streams
Stream Type Base Class Used For
Byte Streams Input/OutputStream Binary data
Character Streams Reader/Writer Text data
java
CopyEdit
FileInputStream fin = new FileInputStream("file.txt"); // Byte
FileReader fr = new FileReader("file.txt"); // Char
✅ 10. FileWriter and FileReader
🔹 Example:
java
CopyEdit
FileWriter fw = new FileWriter("example.txt");
fw.write("Hello World");
fw.close();
FileReader fr = new FileReader("example.txt");
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
fr.close();
📘 Summary Sheet (Quick Revision)
Topic Use/Concept
Logical grouping, access outer
Nested Classes
members
Exception Handling Manage runtime errors gracefully
Thread & Runnable Multitasking in Java
Avoids data corruption with multiple
Synchronization
threads
I/O Streams Reading/writing files in Java
FileWriter/FileReader Character-based file handling