No enclosing instance of type Hoge is accessible.

Overview

I got an error when I wrote this code, so make a note.

public class Hoge {
    private class Fuga(){
        public String name;
    }
    public static void main(String[] args) {
        Fuga fuga = new Fuga();
    }
}
No enclosing instance of type Hoge is accessible. 
Must qualify the allocation with an enclosing instance of type Hoge 
(e.g. x.new A() where x is an instance of Hoge).

I can't access the inside of the Hoge class Error!

Solution

Make Fuga a static class

public class Hoge {
    //Make it a static class
    private static class Fuga(){
        public String name;
    }
    public static void main(String[] args) {
        Fuga fuga = new Fuga();
    }
}

Generate Hoge and then Fuga

public class Hoge {
    private static class Fuga(){
        public String name;
    }
    public static void main(String[] args) {
        //Instantiate Hoge and then instantiate Fuga
        Hoge hoge = new Hoge();
        Fuga fuga = hoge.new Fuga();
        //You can do this
        Fuga fuga2 = new Hoge().new Fuga();
    }
}

Recommended Posts

No enclosing instance of type Hoge is accessible.
What is it? ~ 3 types of "no" ~
Is there no type in Ruby?
No qualifying bean of type error handling
About Enclosing Instance 2