티스토리 뷰
In this post, we're going to look at Inner Class concept.
I decided to start write this post because when i was studying Builder design pattern, i kept seeing Inner Class or Outer Class Or Nested Class etc.
In this posting, i'll tell you a little of Inner Class, not deep dive version.
There are 4 main types of Inner Class.
1. Member Inner Class
2. Method-Local Inner Class
3. Static Nested Class
4. Anonymous Inner Class
But, This posting is going to handle 3 types of Inner Class except Anonymous Inner Class. (Just because! haha)
Before getting into these, we should take a look the basic structure and feature of Inner Class.
class OuterClass {
// Outer class members
class InnerClass {
// Inner class members
}
}
As you can see, InnerClass is inside the OuterClass. This this basic Syntax.
And the basic feature of Inner Class is like this.
1. The Inner Class has access to all members (including private) of the outer class.
2. But, the Outer Class can access the Inner Class members only through an object of the inner class.
Okay, let's get into the Inner Class right now.
Fist one is Member Class.
1. Member Class
Take a look the simple Member Class code example below.
class Outer {
private int outerVariable = 10;
// Member inner class
class Inner {
void print() {
System.out.println("Outer variable :" + outerVariable);
}
}
}
class helloWorld {
public static void main(String[] args) {
// Creating inner class instance
Outer.Inner innerInstance = new Outer().new Inner();
innerInstance.print();
}
}
A member inner class is a non-static class defined at the member level of another class.
It has access to all members of the outer, including private members.
(After Java 16) Member Inner Class can have static member, only if they don't depend on an outer class class.
To make a Inner Class instance, we should instaiated it with an outer class instance.
➡️ (Syntax) Outer.Inner innerInstance = new Outer().new Inner();
Second one is Mehod-Local Inner Class
2. Mehod-Local Inner Class
Take a look the simple example.
class Outer {
void outerMethod() {
// Method-local inner class
class inner {
void innerMethod() {
System.out.println("innerMethod!");
}
}
inner innerInstance = new inner();
innerInstance.innerMethod();
}
}
class Main {
public static void main(String[] args) {
Outer outer = new Outer();
outer.outerMethod();
}
}
As you can see, a method-local inner class is defined inside a method of the outer class.
It can only be instantiated within the method where it(=inner class) is defined.
And, From Java 8 onwards, method-local inner class can access final or final local variables.
So, you can write a code like this.
class Outer {
void outerMethod() {
String str = "helloWorld";
// Method-local inner class
class inner {
void innerMethod() {
System.out.println("innerMethod!" + str);
}
}
inner innerInstance = new inner();
innerInstance.innerMethod();
}
}
class Main {
public static void main(String[] args) {
Outer outer = new Outer();
outer.outerMethod();
}
}
In this code, a Inner class that inside the Outer class can access effectively "helloWorld"(final local variables) here.
According to a blog i referred to, it says likes this.
It cannot be declared as private, protected, static, or transient.
It can be declared as abstract or final, but not both.
(what the "it" is pointing to now is final local variables in outer class)
But, i can't understand fully now... (i will more details later haha😅)
The last one is Static Nested Class
3. Static Nested Class
A Static Nested Class is a static class defined inside another class.
Before taking a look a example code, let's go over the key features of static nested class.
1. Static Nested Class does NOT have access to instance members of the outer class.
2. But, it can access static members.
Okay, take a look the following example code.
class Outer {
static int staticVar = 10;
int nonStaticVar = 20;
// Static nested class
static class Inner {
void print() {
System.out.println("staticVar" + staticVar);
// System.out.println("nonStaticVar" + nonStaticVar); // It can't access instance members
}
}
}
class HelloWorld {
public static void main(String[] args) {
// No need for outer class instance
// Outer.Inner inner = new Outer().new Inner(); we don't need to declare an outer instance here.
Outer.Inner inner = new Outer.Inner();
inner.print();
}
}
At the "print()" method in Inner class, Inner class can't access instance member(nonStaticVar), only it can static members.
But, you may wonder why the following two codes is different.
Outer.Inner inner2 = new Outer().new Inner();
Outer.Inner inner1 = new Outer.Inner();
The main difference of these is whether we created an Outer Class instance or not.
At the first line, we created Outer Class instance and created Inner Class instance.
And at the second line, we created Inner Class instance without creating Outer Class instance.
When the Inner Class is Non-static class, the first line code is correct, but the second is not.
The second one is only possible in Static class.
Why?
In the Non-static class, inner class always belongs to specific instance of outer class.
But, in the static class, inner class doesn't belongs to specific instance of outer class.
That's it 😁.
That's all for this post.
I will go over when to use inner class later. 🥶
reference)
https://www.geeksforgeeks.org/java/inner-class-java/
Inner Class in Java - GeeksforGeeks
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
www.geeksforgeeks.org
'Java' 카테고리의 다른 글
| A Pointer for understanding the difference of Primitive Data and Reference Data (0) | 2026.02.11 |
|---|---|
| Java Anonymous Class (0) | 2026.01.22 |
| 자바 - JVM, Javac, JDK (0) | 2025.12.09 |
| 인텔리제이 글자 크기 확대/축소 단축기 설정 ( ctrl + +, ctrl + - ) (0) | 2025.12.01 |
| 자바 - 배열 요소 비교 방법, 배열 요소가 null인지 확인 방법 (0) | 2025.11.22 |