jef
jef

Reputation: 23

Is it possible to create object of an inner class inside a static class in java?

How can I declare an object of an inner class inside a static class in java?

public class xyz
{
    static class abc
    {
        ...
        // I want to declare an object of class a here. how can I do this?

    }
    class a
    {
        ...
    }
}

Upvotes: 1

Views: 126

Answers (1)

Jesper
Jesper

Reputation: 206786

Instances of inner classes exist in the context of an instance of the enclosing class. So you must first create an instance of the enclosing class, and from there you can create an instance of the inner class. For example:

public class xyz {
    static class abc {
        a member = new xyz().new a();
    }

    class a {
    }
}

More information: Oracle Java Tutorials - Nested Classes

Upvotes: 2

Related Questions