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
An instance inner class (also called a non-static nested class) is a class defined within another class, known as the outer class. Instance inner classes are not declared as static, and they have access to the members (including private members) of the outer class. In this tutorial, we'll discuss the basics of instance inner classes in Java, including their use cases and how to create and access their objects.
To create an instance inner class, simply define the class inside the outer class without using the static
keyword.
Example:
OuterClass.java:
public class OuterClass { private String message = "Hello, World!"; class InnerClass { public void displayMessage() { System.out.println("Message from outer class: " + message); } } }
In this example, we create an outer class called OuterClass
with a private member message
. We then define an instance inner class called InnerClass
within the outer class. The InnerClass
has access to the message
member of the outer class, even though it is private.
To create and access an instance inner class object, you need to first create an object of the outer class, then use that object to create an object of the inner class.
Main.java:
public class Main { public static void main(String[] args) { OuterClass outer = new OuterClass(); OuterClass.InnerClass inner = outer.new InnerClass(); inner.displayMessage(); // Message from outer class: Hello, World! } }
In this example, we first create an object of the OuterClass
called outer
. We then use the outer
object to create an object of the InnerClass
called inner
. Finally, we call the displayMessage()
method on the inner
object, which displays the message from the outer class.
Instance inner classes are useful in the following scenarios:
In conclusion, instance inner classes in Java are a powerful way to create classes that are closely related to their outer class, either logically or because they need access to the outer class's members. By understanding the basics of instance inner classes and knowing how to create and access their objects, you can leverage this feature to write cleaner and more organized code.
Creating and using instance inner classes in Java
// Outer class class Outer { private int outerVar = 10; // Instance Inner Class class Inner { void display() { System.out.println("Outer variable: " + outerVar); } } } // Usage Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.display();
Accessing outer class members from instance inner class
class Outer { private int outerVar = 10; class Inner { void displayOuterVar() { System.out.println("Outer variable: " + outerVar); } } }
Inheritance and polymorphism with instance inner classes
class Outer { class Inner { void display() { System.out.println("Display from Inner class"); } } } // Inheritance and polymorphism class Subclass extends Outer.Inner { @Override void display() { System.out.println("Display from Subclass"); } }
Visibility and encapsulation in Java instance inner classes
class Outer { private int outerVar = 10; // Instance Inner Class class Inner { private int innerVar = 20; void display() { System.out.println("Outer variable: " + outerVar); System.out.println("Inner variable: " + innerVar); } } }
Local variables and instance inner classes in Java
class Outer { void outerMethod() { int localVar = 30; // Instance Inner Class accessing local variable class Inner { void displayLocalVar() { System.out.println("Local variable: " + localVar); } } Inner inner = new Inner(); inner.displayLocalVar(); } }