0% found this document useful (0 votes)
13 views

Nested and Inner Classes in Java

Nested and Inner Classes in Java

Uploaded by

ashwini bhosale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Nested and Inner Classes in Java

Nested and Inner Classes in Java

Uploaded by

ashwini bhosale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Nested and Inner Classes in Java

1. Nested Classes:
A nested class is a class defined within another class. In Java, nested classes are used to logically
group classes that are only used in one place. They increase encapsulation and can lead to more
readable and maintainable code.
Types of Nested Classes:
 Static Nested Class:
o It is declared with the static keyword.
o Since it's static, it can access the outer class's static members, but not non-static
members directly.
o It does not require an instance of the outer class to be instantiated.
o Example:
class OuterClass {
static int outerStaticVar = 10;

static class StaticNestedClass {


void display() {
System.out.println("Outer static variable: " + outerStaticVar);
}
}
}

public class Main {


public static void main(String[] args) {
OuterClass.StaticNestedClass nestedObj = new OuterClass.StaticNestedClass();
nestedObj.display();
}
}
 Non-Static Nested Class (Inner Class):
o It does not have the static keyword.
o It has access to both static and non-static members of the outer class.
o Requires an instance of the outer class to be instantiated.
o Example:
class OuterClass {
int outerVar = 10;

class InnerClass {
void display() {
System.out.println("Outer variable: " + outerVar);
}
}
}

public class Main {


public static void main(String[] args) {
OuterClass outerObj = new OuterClass();
OuterClass.InnerClass innerObj = outerObj.new InnerClass();
innerObj.display();
}
}
2. Inner Classes:
Inner classes are non-static nested classes. They have access to the outer class's instance variables
and methods. There are several types of inner classes:
 Member Inner Class:
o A class defined inside another class and treated like a member of that class.
o Can access all members of the outer class, including private members.
 Local Inner Class:
o Defined inside a block, usually inside a method.
o Local to the block in which it is defined and cannot be accessed outside of it.
o Can access final or effectively final variables of the method.
 Anonymous Inner Class:
o A class without a name and is declared and instantiated all in one place.
o Often used to provide implementation for an interface or to override a method of a
class on the fly.
o Example:
abstract class Animal {
abstract void makeSound();
}

public class Main {


public static void main(String[] args) {
Animal dog = new Animal() {
void makeSound() {
System.out.println("Bark");
}
};
dog.makeSound();
}
}
Modifiers and Access Control in Java
Java provides several modifiers that define the scope and behavior of classes, methods, and
variables.
1. Access Modifiers:
These control the visibility of classes, methods, and variables.
 public:
o Accessible from any other class.
o If a class is declared public, it must be the only class in the file, and the file name
must match the class name.
 private:
o Accessible only within the declared class.
o Cannot be applied to classes (only inner classes can be private).
o Useful for encapsulating the internal workings of a class.
 protected:
o Accessible within the same package and by subclasses in other packages.
o Provides more access than private but is more restrictive than public.
 default (no modifier):
o Also known as package-private.
o Accessible only within the same package.
o If no access modifier is specified, this is the default behavior.
2. Non-Access Modifiers:
These modify behavior rather than access:
 static:
o Belongs to the class rather than instances of the class.
o Can be applied to methods, variables, blocks, and nested classes.
o Static members can be accessed without creating an instance of the class.
 final:
o Applied to classes, methods, and variables.
o If a class is final, it cannot be subclassed.
o If a method is final, it cannot be overridden by subclasses.
o If a variable is final, it can only be initialized once.
 abstract:
o Applied to classes and methods.
o An abstract class cannot be instantiated; it is meant to be subclassed.
o An abstract method has no body and must be implemented by subclasses.
 synchronized:
o Applied to methods and blocks.
o Ensures that a method or block is accessed by only one thread at a time.
o Used in multithreading to avoid thread interference.
 volatile:
o Applied to variables.
o Indicates that a variable's value will be modified by different threads.
o Ensures that changes made in one thread are visible to others.
 transient:
o Applied to variables.
o Prevents serialization of the variable.
o Used in the context of serializing objects.
 native:
o Applied to methods.
o Indicates that the method is implemented in native code using a language like C or
C++.
Understanding how to use nested and inner classes, along with the appropriate modifiers, is
essential for writing effective Java programs that are secure, maintainable, and performant.

You might also like