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

2

1 Answer 1

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 {
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.