Java - No enclosing instance of type Foo is accessible

后端 未结 5 1080
再見小時候
再見小時候 2020-11-21 06:35

I have the following code:

class Hello {
    class Thing {
        public int size;

        Thing() {
            size = 0;
        }
    }

    public stat         


        
相关标签:
5条回答
  • 2020-11-21 06:44

    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");
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 06:47

    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:

    1. 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.

    2. It can make code more maintainable as the classes are logically grouped together around where they are needed.

    3. The inner class has access to the instance variables and methods of its containing class.

    We have mainly three types of Inner Classes

    1. Local inner
    2. Static Inner Class
    3. 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

    0 讨论(0)
  • 2020-11-21 06:53

    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.

    0 讨论(0)
  • 2020-11-21 06:53

    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)

    0 讨论(0)
  • 2020-11-21 07:10

    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
    
    0 讨论(0)
提交回复
热议问题