358

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:

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

Any ideas?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
coolpapa
  • 3,695
  • 2
  • 16
  • 8
  • possible duplicate of [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) – Strelok Mar 05 '12 at 01:35

11 Answers11

551

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.

jacobm
  • 13,790
  • 1
  • 25
  • 27
  • 1
    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? – mr5 Oct 19 '15 at 03:35
  • 1
    No. Every object of an inner class must have a parent, but a parent can have any number of children including 0. – jacobm Oct 19 '15 at 14:08
107

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)

Foggzie
  • 9,691
  • 1
  • 31
  • 48
helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • Your answer is both comprehensive and synthetic. Even if it feels strange (to me at least), the syntax is correct. – boumbh Aug 16 '14 at 09:35
  • If anyone is still getting errors, the syntax `Thing thing1 <>.new Thing()` is the key. I spent a few minutes confused using the syntax `Thing thing1 new <>.Thing()`. =P – nikodaemus Jan 15 '15 at 14:36
  • 1
    @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. – Abandoned Cart Jul 27 '16 at 06:03
27

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

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
12

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
Community
  • 1
  • 1
David Harkness
  • 35,992
  • 10
  • 112
  • 134
9

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");

        }
    }
}
AZ_
  • 21,688
  • 25
  • 143
  • 191
4

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!");
    }
}
Łukasz Rzeszotarski
  • 5,791
  • 6
  • 37
  • 68
0

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);
}
}

}

Maged Almaweri
  • 312
  • 4
  • 11
0

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

Johann
  • 65
  • 7
0
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!");
    }
}
Mark
  • 1
  • 2
    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](/help/how-to-answer). – Community Feb 08 '22 at 16:08
0

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

Rabhi salim
  • 486
  • 5
  • 17
0

you dont actually have to declare anything as static, but you do have to specify some enclosing instance, either implicitly by being in the same class, explicitly before the new, or explicitly in the constructor.

class A {

    class B { 
        final A a = A.this;
    }

    class C extends B { }

    final B b = new B();
    final C c = new C();

}

class D extends A.B {

    public D(A a) {
        a.super();
    }

}

class E { 

    final A   a =     new A();
    final A.B b =   a.new B();
    final A.C c = b.a.new C();
    final D   d =     new D(a);

}
aepurniet
  • 1,719
  • 16
  • 24