0% found this document useful (0 votes)
6 views12 pages

Java Nested Classes and StringBuffer Guide

The document provides an overview of nested classes in Java, including static nested classes, inner classes, and anonymous inner classes, highlighting their definitions, use cases, and key features. It also discusses the StringBuffer class for mutable strings and the tokenizer for breaking strings into tokens. Examples are provided for each concept to illustrate their practical applications.

Uploaded by

dahiyaneha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Java Nested Classes and StringBuffer Guide

The document provides an overview of nested classes in Java, including static nested classes, inner classes, and anonymous inner classes, highlighting their definitions, use cases, and key features. It also discusses the StringBuffer class for mutable strings and the tokenizer for breaking strings into tokens. Examples are provided for each concept to illustrate their practical applications.

Uploaded by

dahiyaneha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Nested classes,

Inner classes,
Anonymous Inner classes,
String Buffer Class,
tokenizer
1. Nested Classes in Java
📌 Definition: A nested class is a class that is declared inside another class.
🔹 Why use nested classes?
• Improves encapsulation (hides details of inner logic).
• Groups related classes together for better organization.
• Provides better access control (inner class can access private members of the outer class).
🔹 Types of Nested Classes:
[Link] Nested Class → A static class inside another class.
[Link] Class → A non-static class inside another class.
[Link] Inner Class → A class without a name that is defined and instantiated in a
single statement.
[Link] Inner Class → A class inside a method.
2. Inner Classes in Java
📌 Definition: An inner class is a type of nested class that is non-static and
is associated with an instance of the outer class.
🔹 Key Points:
•It has access to all members (including private members) of the outer
class.
•It is useful when a class is tightly bound to its enclosing class.
•To create an object of an inner class, an object of the outer class is
required.
🔹 Example Use Case:
•If you have a class Car and want to define a Engine class inside it, you can
use an inner class because an engine belongs to a specific car.
3. Anonymous Inner Classes in Java
📌 Definition: An anonymous inner class is a class that does not have a name
and is declared and instantiated in a single expression.
🔹 Key Points:
•Used to create one-time use classes, mostly for short-lived objects.
•Commonly used when implementing interfaces or extending classes without
creating a separate subclass.
•Cannot have a constructor since it has no name.
🔹 Example Use Case:
•When you need to override a method of an interface without creating a
separate named class (e.g., implementing a button click listener in GUI
applications).
4. StringBuffer Class in Java
📌 Definition: The StringBuffer class is used to create mutable (modifiable) strings.
🔹 Why use StringBuffer instead of String?
•String objects are immutable, meaning every time you modify a String, a new object is
created.
•StringBuffer allows modifications without creating new objects, improving performance
in cases of frequent string manipulations.
🔹 Key Features of StringBuffer:
[Link] → You can modify its content.
[Link]-safe → It is synchronized, meaning it can be used safely in multi-threaded
environments.
[Link] like append(), insert(), replace(), reverse(), delete() help in modifying strings.
🔹 Example Use Case:
•Useful in applications where string values change frequently, such as text editors, logging
systems, and dynamic data formatting.
5. Tokenizer in Java
📌 Definition: A tokenizer is used to break a string into individual pieces (tokens) based on a delimiter (separator).
🔹 Why use a Tokenizer?
•It is useful for parsing structured text like CSV files, log files, or user inputs.
•Helps in breaking large text into meaningful parts for further processing.
🔹 Example Use Case:
•If you have a sentence "Java is fun", a tokenizer can split it into words: ["Java", "is", "fun"].
•If you have a CSV data row "Alice,23,USA", a tokenizer can extract individual values: ["Alice", "23", "USA"].
🔹 Types of Tokenizers in Java:
[Link] (Legacy, found in [Link] package).
[Link]() method of String class (Most commonly used).
[Link] class (More powerful and flexible for input processing).
1. Nested Classes in Java
📌 Example: Outer Class (Car) with Static Nested Class (Engine)

// Outer class
class Car {
private String model = "Tesla"; o/p:
The car has an engine.
// Static Nested Class
static class Engine {
void show() {
[Link]("The car has an engine.");
}
}
}

// Main class
public class NestedClassExample {
public static void main(String[] args) {
// Creating an object of the static nested class
[Link] engine = new [Link]();
[Link]();
}
}
2. Inner Classes in Java
📌 Example: Outer Class (Car) with Inner Class (Engine)

// Outer class
class Car {
private String model = "Tesla"; Engine belongs to Car model: Tesla

// Inner class (non-static)


class Engine {
void show() {
[Link]("Engine belongs to Car model: " + model);
}
}
}

// Main class
public class InnerClassExample {
public static void main(String[] args) {
Car myCar = new Car(); // Outer class object
[Link] engine = [Link] Engine(); // Creating Inner class object
[Link]();
}
}
3. Anonymous Inner Class in Java
📌 Example: Implementing an Interface Using an Anonymous Inner Class
java

// Interface
interface Animal {
void sound();
}
Dog barks: Woof Woof!
// Main class
public class AnonymousInnerClassExample {
public static void main(String[] args) {
// Anonymous Inner Class implementing Animal interface
Animal dog = new Animal() {
public void sound() {
[Link]("Dog barks: Woof Woof!");
}
};

[Link](); // Calling method of anonymous inner class


}
}
4. StringBuffer Class in Java

📌 Example: Using StringBuffer for Mutable Strings


Hello World!
public class StringBufferExample { Hello Java World!
public static void main(String[] args) { Hello Programming World!
StringBuffer sb = new StringBuffer("Hello"); !dlroW gnimmargorP olleH

[Link](" World!"); // Appends text


[Link](sb);

[Link](6, "Java "); // Inserts text at position 6


[Link](sb);

[Link](6, 10, "Programming"); // Replaces text


[Link](sb);

[Link](); // Reverses the string


[Link](sb);
}
}
5. Tokenizer in Java
📌 Example: Splitting a Sentence into Words Using StringTokenizer

import [Link];
Java
public class TokenizerExample { is
public static void main(String[] args) { a
String sentence = "Java is a powerful language"; powerful
language
// Creating StringTokenizer with space as a delimiter
StringTokenizer tokenizer = new StringTokenizer(sentence,
" ");

while ([Link]()) {
[Link]([Link]());
}
}
}

You might also like