Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, there is no concept of a "partial inner class" like there is for partial classes in C#. However, you can create inner classes, which are classes defined within the scope of another class, often referred to as the outer class. In this tutorial, we will cover the basics of inner classes in Java, including their types and use cases.
There are four types of inner classes in Java:
a. Member inner class b. Static inner class c. Local inner class d. Anonymous inner class
A member inner class is a regular class defined within the scope of an outer class. It has access to all the fields and methods of the outer class, even if they are private. However, it cannot have static members, as it is associated with an instance of the outer class.
Example:
class OuterClass { private String message = "Hello, World!"; class InnerClass { void displayMessage() { System.out.println("Message: " + message); } } }
A static inner class is a class defined within the scope of an outer class with the static
keyword. It behaves like a regular class but has access only to the static fields and methods of the outer class.
Example:
class OuterClass { private static String message = "Hello, World!"; static class StaticInnerClass { void displayMessage() { System.out.println("Message: " + message); } } }
A local inner class is a class defined within a method or a block of code. It has access to the final or effectively final local variables of the method or block in which it is defined.
Example:
class OuterClass { void outerMethod() { final String message = "Hello, World!"; class LocalInnerClass { void displayMessage() { System.out.println("Message: " + message); } } } }
An anonymous inner class is an unnamed class defined within an expression, usually as an argument to a method call. Anonymous inner classes are often used to create objects with specific behavior, such as event listeners or comparators.
Example:
import java.util.Comparator; class Main { public static void main(String[] args) { Comparator<String> stringLengthComparator = new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.length() - s2.length(); } }; } }
In this tutorial, we discussed the basics of inner classes in Java, including their types and use cases. Inner classes are a powerful feature of Java, allowing you to create classes within the scope of other classes, providing encapsulation and organization for your code.
Java Nested Classes Explained:
public class Outer { class Inner { // Inner class } }
Static Inner Class in Java:
public class Outer { static class StaticInner { // Static inner class } }
Inner Class vs Outer Class in Java:
public class Outer { class Inner { // Inner class can access members of Outer } }
Local Inner Class in Java:
public class Outer { void someMethod() { class LocalInner { // Local inner class } } }
Anonymous Inner Class in Java:
public interface MyInterface { void myMethod(); } public class Outer { MyInterface myObject = new MyInterface() { public void myMethod() { // Anonymous inner class implementation } }; }
Java Inner Class Access Modifiers:
public
, protected
, private
, or package-private (default).public class Outer { private class PrivateInner { // Private inner class } }
Java Inner Class and Encapsulation:
public class Outer { private int outerField; class Inner { void accessOuterField() { outerField = 42; // Inner class can access private field of Outer } } }
Java Inner Class vs Lambda Expression:
Runnable runnable = new Runnable() { public void run() { // Inner class implementation } }; Runnable lambdaRunnable = () -> { // Lambda expression implementation };
Java Inner Class Example Code:
public class Team { private String teamName; class Player { private String playerName; Player(String playerName) { this.playerName = playerName; } void displayPlayer() { System.out.println("Team: " + teamName + ", Player: " + playerName); } } Team(String teamName) { this.teamName = teamName; } }
Java Inner Class Inheritance:
public class Outer { class Inner extends MyBaseClass implements MyInterface { // Inner class inheritance } }
Nested Interface in Java:
public class Outer { interface NestedInterface { // Nested interface } }
Inner Class Visibility and Scoping in Java:
public class Outer { private int outerField; void someMethod() { class LocalInner { void accessOuterField() { outerField = 42; // Local inner class can access private field of Outer } } } }