[JAVA] I investigated the enclosing instance.

https://qiita.com/hp_kj/items/dd533d98f9d065bd0301 When I searched for this, the word "enclosing instance" came up, so I looked it up.

According to Java A2Z An "instance of an outer class" for an inner class.   An instance of an inner class is a special instance that is "contained within an instance of an outer class". Therefore, in order to create an instance of the inner class, an instance of the outer class is always required. The "outer class instance" is the "enclosing instance". An instance of an inner class is an "enclosing" or "enclosing" instance.

"Outer class instance" is "Enclosing instance"

First of all, this seems to be the conclusion. And As a meaning

An instance of an inner class is an "enclosing" or "enclosing" instance.

about it. Applying to the previous code,

public class Main {
    public static void main(String []args) {
        Outer o =  new Outer();///1-1
        Outer.Inner oi = o.new Inner();///2-1
        oi.innerPrint();
    }

    public static class Outer{///1

        int outerhp = 10;

        public class Inner{///2

            public void innerPrint() {
                System.out.println(Outer.this.outerhp);
            }

        }

    }

}

I am creating an instance of external class 1 (Outer) in 1-1. Then, in 2-1 I created an instance of inner class 2 (Inner) included in the instance of the outer class.

Recommended Posts

I investigated the enclosing instance.
I investigated the internal processing of Retrofit
About Enclosing Instance 2
I investigated test automation
I investigated the super that I met with the devise controller
[Rails] I investigated the difference between redirect_to and render.
I summarized the collection framework.
I investigated Java primitive types
I tried the Docker tutorial!
I tried the VueJS tutorial!
I read the Kotlin startbook
I tried the FizzBuzz problem
[Java] I implemented the combination.
I studied the constructor (java)
I investigated the mechanism of attr_accessor (* Hoge :: ATTRIBUTES) that I sometimes see
[Ruby] This is the solution. When should I use instance variables?
I took a look at the resources of Azure Container Instance
"RSpec doesn't work!" The cause was spring, so I investigated it.
Compound descriptor: I calculated the CDK
I read the source of ArrayList I read
I read the source of Integer
I tried to explain the method
I read the source of Long
I tried the Java framework "Quarkus"
[Rails] I tried deleting the application
The story I wanted to unzip
I read the source of Short
I read the source of Byte
I read the source of String
[Spring Boot] I investigated how to implement post-processing of the received request.
[Swift] I investigated the variable expansion "\ ()" that realizes the mysterious function of Swift UI.