No enclosing instance of type Foo is accessible

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?


    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.


    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.

  • Change Thing to be a static nested class.

    static class Thing
    
  • Create an instance of Hello, then create 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
    }
    
  • Move Thing out of the Hello class.

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


    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

    链接地址: http://www.djcxy.com/p/88886.html

    上一篇: jLabels自动换行

    下一篇: 没有可以访问Foo类型的封闭实例