0

This is my main class:

import java.util.ArrayList;
public class MainClass {
    public static void main(String[] args){
        ArrayList<SecondClass.InnerClass> list=new ArrayList<SecondClass.InnerClass>();
        list.add(new SecondClass.InnerClass()); //error here (read below)
    }
}       

Here is the second class:

public class SecondClass {
    public class InnerClass{

    }
}

At MainClass, at list.add, I get this error:

No enclosing instance of type SecondClass is accessible. Must qualify the allocation with an enclosing instance of type SecondClass (e.g. x.new A() where x is an instance of SecondClass).

I need to have InnerClass non-static because InnerClass needs to make a static reference to a non-static method. How can I add elements in the ArrayList?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
eliaS
  • 35
  • 1
  • 6

4 Answers4

7

I think you need:

new SecondClass().new InnerClass()
Daniel Stanley
  • 1,050
  • 6
  • 15
  • Either that, or you can make InnerClass static. Otherwise, Daniel is right, in order to instantiate InnerClass, you'll need a live instance of SecondClass to do it. – Michael Krause Oct 10 '14 at 16:14
1

I'd do some reading up on nested classes, and in particular the differences between static and non-static nested classes.

If you choose to make InnerClass a static nested class, note the following:

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

This means you don't need an instance of SecondClass in order to make an instance of InnerClass - You can instantiate it as you do at the moment.

If however you make InnerClass a non-static nested class (I believe these are sometimes referred to as inner classes, but double check this terminology), you need to create an instance of SecondClass in order to create an instance of InnerClass:

new SecondClass().new InnerClass()
Edd
  • 3,724
  • 3
  • 26
  • 33
  • @eliaS Notice that `new SecondClass().new InnerClass()` creates new instance of outer class (`SecondClass`) and then inner class (`InnerClass`). So if you put this code in loop you will end up with same number of new instances of `SecondClass` and `InnerClass` classes. This seems like overdoing things because before loop you could create instance of `SecondClass` class (`SecondClass outer = new SecondClass();`) and then inside loop create instances of `InnerClass` using this one outer instance (`outer.new InnerClass()`) which will end up with on outer, and N inner instances. – Pshemo Oct 10 '14 at 16:31
1

When you want to use an innerClass you must create an instance of the class it contain innerclass. After that you'll use it. Example how to use an innerclass following the tutorial of Oracle:

SecondClass sc=new SecondClass();
SecondClass.InnerClass in=sc.new InnerClass();

And you can see detail here: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

0

Make InnerClass static

public class SecondClass {
    public static class InnerClass{

    }
}
ToYonos
  • 16,469
  • 2
  • 54
  • 70