ChatGPT解决这个技术问题 Extra ChatGPT

What causes error "No enclosing instance of type Foo is accessible" and how do I fix it?

I have the following code:

class Hello {
    class Thing {
        public int size;

        Thing() {
            size = 0;
        }
    }

    public static void main(String[] args) {
        Thing thing1 = new Thing();
        System.out.println("Hello, World!");
    }
}

I know Thing does nothing, but my Hello, World program compiles just fine without it. It's only my defined classes that are failing on me.

And it refuses to compile. I get No enclosing instance of type Hello is accessible." at the line that creates a new Thing. I'm guessing either:

I have system level problems (either in DrJava or my Java install) or I have some basic misunderstanding of how to construct a working program in java.

Any ideas?


j
jacobm

static class Thing will make your program work.

As it is, you've got Thing as an inner class, which (by definition) is associated with a particular instance of Hello (even if it never uses or refers to it), which means it's an error to say new Thing(); without having a particular Hello instance in scope.

If you declare it as a static class instead, then it's a "nested" class, which doesn't need a particular Hello instance.


Does this also mean that if I instantiate the outer class, the non-static inner class would be created as well even if I don't use it anywhere?
No. Every object of an inner class must have a parent, but a parent can have any number of children including 0.
F
Foggzie

You've declared the class Thing as a non-static inner class. That means it must be associated with an instance of the Hello class.

In your code, you're trying to create an instance of Thing from a static context. That is what the compiler is complaining about.

There are a few possible solutions. Which solution to use depends on what you want to achieve.

Move Thing out of the Hello class.

Change Thing to be a static nested class. static class Thing

Create an instance of Hello before creating an instance of Thing. public static void main(String[] args) { Hello h = new Hello(); Thing thing1 = h.new Thing(); // hope this syntax is right, typing on the fly :P }

The last solution (a non-static nested class) would be mandatory if any instance of Thing depended on an instance of Hello to be meaningful. For example, if we had:

public class Hello {
    public int enormous;

    public Hello(int n) {
        enormous = n;
    }

    public class Thing {
        public int size;

        public Thing(int m) {
            if (m > enormous)
                size = enormous;
            else
                size = m;
        }
    }
    ...
}

any raw attempt to create an object of class Thing, as in:

Thing t = new Thing(31);

would be problematic, since there wouldn't be an obvious enormous value to test 31 against it. An instance h of the Hello outer class is necessary to provide this h.enormous value:

...
Hello h = new Hello(30);
...
Thing t = h.new Thing(31);
...

Because it doesn't mean a Thing if it doesn't have a Hello.

For more information on nested/inner classes: Nested Classes (The Java Tutorials)


Your answer is both comprehensive and synthetic. Even if it feels strange (to me at least), the syntax is correct.
If anyone is still getting errors, the syntax Thing thing1 <<HelloInstantiation>>.new Thing() is the key. I spent a few minutes confused using the syntax Thing thing1 new <<HelloInstantiation>>.Thing(). =P
@skia.heliou Thanks! I was making a helper class and static variables (or even classes) not only eliminate scope, but they are often inefficient. Needing to provide runtime arguments makes it a bother to use a main method. This was all I needed and exactly what I was looking to find.
R
Rupesh Yadav

Well... so many good answers but i wanna to add more on it. A brief look on Inner class in Java- Java allows us to define a class within another class and Being able to nest classes in this way has certain advantages:

It can hide(It increases encapsulation) the class from other classes - especially relevant if the class is only being used by the class it is contained within. In this case there is no need for the outside world to know about it. It can make code more maintainable as the classes are logically grouped together around where they are needed. The inner class has access to the instance variables and methods of its containing class.

We have mainly three types of Inner Classes

Local inner Static Inner Class Anonymous Inner Class

Some of the important points to be remember

We need class object to access the Local Inner Class in which it exist.

Static Inner Class get directly accessed same as like any other static method of the same class in which it is exists.

Anonymous Inner Class are not visible to out side world as well as to the other methods or classes of the same class(in which it is exist) and it is used on the point where it is declared.

Let`s try to see the above concepts practically_

public class MyInnerClass {

public static void main(String args[]) throws InterruptedException {
    // direct access to inner class method
    new MyInnerClass.StaticInnerClass().staticInnerClassMethod();

    // static inner class reference object
    StaticInnerClass staticInnerclass = new StaticInnerClass();
    staticInnerclass.staticInnerClassMethod();

    // access local inner class
    LocalInnerClass localInnerClass = new MyInnerClass().new LocalInnerClass();
    localInnerClass.localInnerClassMethod();

    /*
     * Pay attention to the opening curly braces and the fact that there's a
     * semicolon at the very end, once the anonymous class is created:
     */
    /*
     AnonymousClass anonymousClass = new AnonymousClass() {
         // your code goes here...

     };*/
 }

// static inner class
static class StaticInnerClass {
    public void staticInnerClassMethod() {
        System.out.println("Hay... from Static Inner class!");
    }
}

// local inner class
class LocalInnerClass {
    public void localInnerClassMethod() {
        System.out.println("Hay... from local Inner class!");
    }
 }

}

I hope this will helps to everyone. Please refer for more


Suggested improvements: name the first class MyOuterClass, remove the comments from around AnonymousClass.
As noted earlier, the statements re anonymousClass are not valid, as the assignment will fail.
C
Community

Thing is an inner class with an automatic connection to an instance of Hello. You get a compile error because there is no instance of Hello for it to attach to. You can fix it most easily by changing it to a static nested class which has no connection:

static class Thing

A
AZ_

Lets understand it with the following simple example. This happens because this is NON-STATIC INNER CLASS. You should need the instance of outer class.

 public class PQ {

    public static void main(String[] args) {

        // create dog object here
        Dog dog = new PQ().new Dog();
        //OR
        PQ pq = new PQ();
        Dog dog1 = pq.new Dog();
    }

    abstract class Animal {
        abstract void checkup();
    }

    class Dog extends Animal {
        @Override
        void checkup() {
            System.out.println("Dog checkup");

        }
    }

    class Cat extends Animal {
        @Override
        void checkup() {
            System.out.println("Cat Checkup");

        }
    }
}

Ł
Łukasz Rzeszotarski

Before Java 14 You have to add static keyword to access class Thing from the static context.

class Hello {
    static class Thing {
        public int size;

        Thing() {
            size = 0;
        }
    }

    public static void main(String[] args) {
        Thing thing1 = new Thing();
        System.out.println("Hello, World!");
    }
}

Java 14+ Starting from Java 14 you can use inner record classes they are implicitly static. So you would have:

class Hello {
    record Thing(int size) { }

    public static void main(String[] args) {
        Thing thing1 = new Thing(0);
        System.out.println("Hello, World!");
    }
}

note that size would be final when using a record.
M
Maged Almaweri

Declare the INNER class Thing as a static and it will work with no issues.

I remember I have the same issue with the inner class Dog when I declared it as class Dog { only. I got the same issue as you did. There were two solutions:

1- To declare the inner class Dog as static. Or

2- To move the inner class Dog to a new class by itself.

Here is the Example:

public class ReturnDemo {

public static void main(String[] args) {
    
    int z = ReturnDemo.calculate(10, 12);
    System.out.println("z = " + z);
    
    ReturnDemo.Dog dog = new Dog("Bosh", " Doggy");
    System.out.println( dog.getDog());
}


public static int calculate (int x, int y) {
    return x + y;
}

public void print( ) {
    System.out.println("void method");
    return;
}

public String getString() {
    return "Retrun String type value";
}


static class Dog {
    
private String breed;
private String name;

public Dog(String breed, String name) {
    super();
    this.breed = breed;
    this.name = name;
}

public Dog getDog() {
    // return Dog type;
    return this;
    
}

public String toString() {
    return "breed" + breed.concat("name: " + name);
}
}

}


J
Johann

Try this my friend: (you can also call it Hello instead of Main)

class Thing {
  public int size;

    Thing() {
      size = 0;
    }
}


class Main {
  public static void main(String[] args) {
    Thing thing1 = new Thing();
      System.out.println("Hello, World!");
  }
}

The idea behind this is that you have to create a separate class to include the static void main (String[] args) method. Summing up: you must have a class that will create your objects, and another class (outside the previous one) in which you'll include the object creation. If you call it Main, you should have a file called Main.java. If you want to call it Hello, then your file must be named Hello.java


M
Mark
class Hello {
    class Thing {
        public int size;

        Thing() {
            size = 0;
        }
    }

    public static void main(String[] args) {
        
        PlayGround obj = new PlayGround();
        
        Thing obj2 = obj.new Thing();
        
        System.out.println("Hello, World!");
    }
}

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
R
Rabhi salim

In my case it was because of one extra '}'