yonan2236
yonan2236

Reputation: 13649

Can't create an instance of a Class

What's the reason why - an enclosing instance that contains appears when trying to instantiate a class?

Below is my actual code:

public static void main(String[] args) {
    InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.Item_Type();  
    // Error: an enclosing instance that contains InterspecTradeItems_Type.Item_Type is required

}


public class InterspecTradeItems_Type {
    public class Item_Type {

    }
}

Thanks.

Upvotes: 0

Views: 4918

Answers (6)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279910

Assuming InterspecTradeItems_Type is declared/defined in a class called Main, you need

InterspecTradeItems_Type.Item_Type item = new Main(). 
                                new InterspecTradeItems_Type().new Item_Type();  

You have an inner class within and inner class. You need an instance of each outer class to get to it.

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68915

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

So either use above syntax or make Item_Type class static

public static void main(String[] args) {
    InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.
                                                                   Item_Type();  
    // Error: an enclosing instance that contains
                               InterspecTradeItems_Type.Item_Type is required

}    
public class InterspecTradeItems_Type {
    public static class Item_Type {

    }
}

Read docs on Inner classes for more information.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

Since Item_Type class is not a static nested class, but an inner class of InterspecTradeItems_Type, you need an instance of the later to access the former.

So, to create an instance of the inner class, you should create instance of the enclosing class:

new InterspecTradeItems_Type().new Item_Type(); 

Of course another option is to make Item_Type a static class:

public class InterspecTradeItems_Type {
    public static class Item_Type {

    }
}

And then your code would work just fine.

Upvotes: 2

Dax Joshi
Dax Joshi

Reputation: 143

The correct way to have an object of inner class is

    InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.new Item_Type();  

Upvotes: 0

sunysen
sunysen

Reputation: 2351

try

public static void main(String[] args) {
    InterspecTradeItems_Type item = new InterspecTradeItems_Type();
    Item_Type item1 = item.new Item_Type();  

}

Upvotes: 0

bNd
bNd

Reputation: 7630

As Item_Type is inner class. To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type().new Item_Type();

Upvotes: 2

Related Questions