0

I have a program with multiple classes, and when I try to make an instance of one of these objects in main, I get an error. How do I properly create a class in main with multiple classes?

public class A {

    class B {
    }

    class C {
    }

    public static void main(String[] args) {
        B b = new B();
        C c = new C();
    }

Error: No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A

user2943615
  • 113
  • 3
  • 7
  • you can make the inner classes static, then they don't need an enclosing instance. – eckes Jan 07 '15 at 23:56
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 03 '16 at 23:57

1 Answers1

1

This is because B and C are inner classes. Unless you understand inner classes, this is probably not what you want.

Move them outside A:

public class A {
    public static void main(String[] args) {
        B b = new B();
        C b = new C();
    }
}
class B {
}
class C {
}
user253751
  • 57,427
  • 7
  • 48
  • 90