0

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.

yonan2236
  • 13,371
  • 33
  • 95
  • 141
  • possible duplicate of [An enclosing instance that contains is required](http://stackoverflow.com/questions/4297857/an-enclosing-instance-that-contains-my-reference-is-required) – Joshua Taylor Sep 26 '13 at 12:37
  • If you don't need an instance of the enclosing class in the Item_Type, you should make it static. – NeplatnyUdaj Sep 26 '13 at 12:38

6 Answers6

2

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    ... or make Item_Type static. Also note that `Item_Type` *is* a nested class. All inner classes are nested by definition. JLS section 8.1.3: "An inner class is a nested class that is not explicitly or implicitly declared static." – Jon Skeet Sep 26 '13 at 12:36
  • Note that because `InterspecTradeItems_Type` is not `static` you need an instance of its outer class to instantiate it. – Sotirios Delimanolis Sep 26 '13 at 12:41
  • @SotiriosDelimanolis. From where did you infer that `InterspecTradeItems_Type` is an inner class of some outer class? – Rohit Jain Sep 26 '13 at 12:43
  • It seems to be in the same class body as `main`. – Sotirios Delimanolis Sep 26 '13 at 12:44
  • @SotiriosDelimanolis. We don't know that for sure. But anyways, if that is the case, then yes another enclosing instance would be needed. – Rohit Jain Sep 26 '13 at 12:45
2

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

bNd
  • 7,512
  • 7
  • 39
  • 72
1

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

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.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

try

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

}
sunysen
  • 2,265
  • 1
  • 12
  • 13
0

The correct way to have an object of inner class is

    InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.new Item_Type();  
Dax Joshi
  • 143
  • 11