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

Understanding Java Inner Classes and Wrappers

Uploaded by

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

Understanding Java Inner Classes and Wrappers

Uploaded by

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

Introduction to Inner Class

An Inner Class is a class that is defined within another class.


It helps group classes that logically belong together and improves encapsulation, readability,
and modularity.

✔ Why use Inner Classes?

 To logically group dependent classes


 To access private members of the outer class easily
 To make code cleaner when a class is used only once
 Useful in event handling (GUI, Android)
 Helps achieve better encapsulation

⭐ Types of Inner Classes

Java supports 4 types of Inner Classes:

1. Member (Non-Static) Inner Class


2. Static Nested Class
3. Local Inner Class
4. Anonymous Inner Class

-------------------------------------------------------------

1️⃣ Member Inner Class (Non-Static Inner Class)

✔ Introduction

A class defined inside another class, but outside any method.


It can access all members (including private) of the outer class.

BankAccount → Transaction
A transaction cannot exist without an account, so the inner class makes logical sense.

✔ Example

class BankAccount {
private String accountNumber = "ACC1001";
private double balance = 5000;

// Member Inner Class


class Transaction {
void deposit(double amount) {
balance += amount;
[Link]("Deposited: " + amount);
[Link]("New Balance: " + balance);
}
}
}

public class Main {


public static void main(String[] args) {
BankAccount account = new BankAccount();

// Creating inner class object


[Link] tx = [Link] Transaction();
[Link](1500);
}
}

✔ Explanation

 Transaction is an inner class of BankAccount.


 It can directly access private balance, improving encapsulation.
 Object of inner class is created using outer class object.

-------------------------------------------------------------

2️⃣ Static Nested Class

✔ Introduction

A class defined inside another class but declared static.


It cannot access non-static outer class members directly.

✔ Example

Company → EmployeeBadge
A badge belongs to the company but doesn't depend on a specific employee instance.

✔ Code Example

class Company {
private static String companyName = "Microsoft";

// Static Nested Class


static class EmployeeBadge {
void printCompany() {
[Link]("Company: " + companyName);
}
}
}

public class Main {


public static void main(String[] args) {
[Link] badge = new [Link]();
[Link]();
}
}

✔ Explanation

 EmployeeBadge is static, so it can be created without an outer class object.


 It can access only static members (companyName).
 Useful for utility/helper classes inside a main class.

-------------------------------------------------------------

3️⃣ Local Inner Class

✔ Introduction

A class declared inside a method, only accessible within that method.


Useful when a helper class is needed temporarily.

✔ Real-Time Example

OrderService → Validator (used only during order processing)

✔ Code Example

class OrderService {

void processOrder() {
String orderId = "ORD7001";

// Local Inner Class


class Validator {
void validate() {
[Link]("Validating Order: " + orderId);
}
}

Validator validator = new Validator();


[Link]();
}
}

public class Main {


public static void main(String[] args) {
new OrderService().processOrder();
}
}

✔ Explanation

 Validator exists only inside processOrder() method.


 It can access effectively final variables (orderId).
 Ideal for temporary, method-specific logic.

-------------------------------------------------------------

4️⃣ Anonymous Inner Class

✔ Introduction

A class without a name, created for immediate use.


Widely used in event handling, callbacks, and thread creation.

✔ Real-Time Example

Button click event listener used in GUI or Android.

✔ Code Example

interface OnClickListener {
void onClick();
}

public class Main {


public static void main(String[] args) {

// Anonymous Inner Class


OnClickListener button = new OnClickListener() {
@Override
public void onClick() {
[Link]("Button Clicked!");
}
};

[Link]();
}
}

✔ Explanation

 No class name is created; only an object is created.


 Used when a class is needed only once.
 Saves code and increases readability.

Type of Inner Can Access Outer Class Real-Time


Where Declared?
Class Instance Members? Scenario
Member Inner Inside class, outside Bank →
Yes
Class methods Transaction
Inside class with static Company →
Static Nested Class Only static members
keyword Badge
Local Inner Class Inside a method Yes (final variables) Order → Validator
Anonymous Inner Button → Click
Inside code block Yes
Class Listener

UNIT-4

What is a Wrapper Class?

A wrapper class in Java is a class that converts a primitive data type into an object.

Java has 8 primitive data types, and each has a corresponding wrapper class.

Wrapper classes are needed because:

 Collections (like ArrayList) work with objects, not primitives


 They support null values
 They provide useful methods (parseInt(), toString(), etc.)
 They allow autoboxing and unboxing

Primitive Types and Their Wrapper Classes

Primitiv Wrapper
e Class
byte Byte

short Short

int Integer

long Long

float Float

double Double

char Character

boolean Boolean
Programming Example for Each Wrapper
Class
Below are small examples showing how each wrapper class is used.

1 Integer (wrapper of int)


int a = 10;
Integer obj = a; // Autoboxing
int b = obj; // Unboxing
[Link]("Integer value = " + obj);

Explanation

 int a = 10; → primitive integer.


 Integer obj = a; → Java automatically converts int to Integer (autoboxing).
 int b = obj; → Java converts Integer object back to primitive (unboxing).
 Prints the wrapper object value (10).

2 Double (wrapper of double)


double d = 12.45;
Double obj = d; // Autoboxing

double e = obj; // Unboxing


[Link]("Double value = " + obj);

Explanation

 double d = 12.45; → primitive double.


 Double obj = d; → primitive double becomes Double object (autoboxing).
 double e = obj; → converted back to primitive double (unboxing).
 Prints the wrapper object value (12.45).

3 Float (wrapper of float)


float f = 3.14f;
Float obj = f; // Autoboxing

float g = obj; // Unboxing


[Link]("Float value = " + obj);
Explanation

 float f = 3.14f; → primitive float.


 Float obj = f; → Java wraps float into Float object.
 float g = obj; → Java unwraps it back to float.
 Output: 3.14.

4 Long (wrapper of long)


long l = 10000L;
Long obj = l;

long m = obj;
[Link]("Long value = " + obj);

Explanation

 long l = 10000L; → primitive long.


 Long obj = l; → converted to a Long object.
 long m = obj; → converted back to primitive long.
 Output: 10000.

5 Short (wrapper of short)


short s = 50;
Short obj = s;
short t = obj;
[Link]("Short value = " + obj);

Explanation

 short s = 50; → primitive short.


 Short obj = s; → short converted to Short object.
 short t = obj; → converted back to primitive short.
 Output: 50.

6 Byte (wrapper of byte)


byte b = 5;
Byte obj = b;

byte c = obj;
[Link]("Byte value = " + obj);
Explanation

 byte b = 5; → primitive byte.


 Byte obj = b; → conversion to Byte object.
 byte c = obj; → unboxed back to byte.
 Output: 5.

7 Character (wrapper of char)


char ch = 'A';
Character obj = ch;

char ch2 = obj;


[Link]("Character value = " + obj);

Explanation

 char ch = 'A'; → primitive character.


 Character obj = ch; → primitive char becomes Character object.
 char ch2 = obj; → unboxing back to primitive char.
 Output: A.

8 Boolean (wrapper of boolean)


boolean flag = true;
Boolean obj = flag;

boolean flag2 = obj;


[Link]("Boolean value = " + obj);

Explanation

 boolean flag = true; → primitive boolean.


 Boolean obj = flag; → converted to Boolean object.
 boolean flag2 = obj; → unboxed into primitive boolean.
 Output: true.

 Wrapper classes convert primitive types into objects.


 They allow primitives to be used in Collections, Generics, and OOP features.
 They support autoboxing (primitive → object) and unboxing (object → primitive).
 Each primitive has a corresponding wrapper class.
Autoboxing — Definition

Autoboxing is the automatic conversion that Java performs when it converts a primitive
data type → its wrapper class object.

✔ Done automatically by Java compiler.

Autoboxing Example
int a = 10; // primitive
Integer obj = a; // autoboxing (int → Integer)

[Link](obj); // Output: 10

Java automatically does this internally:

Integer obj = [Link](a);

Unboxing — Definition

Unboxing is the automatic conversion that Java performs when it converts a wrapper class
object → primitive data type.

✔ Also handled automatically by Java compiler.

Unboxing Example
Integer obj = 20; // wrapper object
int b = obj; // unboxing (Integer → int)

[Link](b); // Output: 20

Java internally converts it like this:

int b = [Link]();

Combined Example (Autoboxing + Unboxing)


public class Demo {
public static void main(String[] args) {

// Autoboxing
int x = 5;
Integer wrappedX = x;

// Unboxing
Integer y = new Integer(15);
int unwrappedY = y;

[Link]("Autoboxing result: " + wrappedX);


[Link]("Unboxing result: " + unwrappedY);
}
}

Concept Meaning Example

Autoboxin primitive → wrapper


Integer obj = 10;
g object

wrapper object →
Unboxing int x = obj;
primitive

You might also like