0

I'm trying to add an array of object in my class(MainActivity), for example

public class MainActivity extends Activity {
    private class A {
      A(String s) { ..}
    }
    private static final A[] aList1;
    private static final List<A> aList2;
    ...

both are ok with me.

But I don't know how to initialize aList1 or aList2. Had already tried following:

private static final A[] aList;
static {
    a = new A[2];
    a[0] = new A("emails");
}

And also tried:

private static final List<A> aList = new ArrayList<A>(){{
    add(new A("emails"));
}};

but eclipse are complaining: No enclosing instance of type MainActivity is accessible. Must qualify the allocation with an enclosing instance of type MainActivity (e.g. x.new A() where x is an instance of MainActivity).

How to fix this?

5
  • What is A? Where is MainActivity? Commented Oct 16, 2013 at 2:22
  • stackoverflow.com/questions/6980199/… Commented Oct 16, 2013 at 2:22
  • It sounds like we're not getting the full story. What's the deal with the inner class? Commented Oct 16, 2013 at 2:28
  • @Deqing I've updated my answer. I think I understood now your problem. Commented Oct 16, 2013 at 2:45
  • Possible duplicate of Java - No enclosing instance of type Foo is accessible Commented Mar 4, 2016 at 0:44

3 Answers 3

1

ArrayList is better than List. It has more methods. Sample:

private static final A[] aList2;
private static final ArrayList<A> aList = new ArrayList<A>(); //you can add in static aList=new ArrayList<a>();

....or...
static {
    aList = new ArrayList(a):
    aList.add(new A("emails"));
}

To convert the array to A[]:

A[] array = new A[aList.size()];
array = aList.toArray(array);

To fast gets value:

for (A item : aList) {
    ... do somme with item
}

To gets any item: aList.get(int index);

Sign up to request clarification or add additional context in comments.

Comments

0

I think I understood your problem now. You have a inner class A declared inside your class MainActivity, right? Well, in this case you wont be able to initialize your static final variables since you gonna need an instance of MainActivity to create a new instance of A. What I suggest you to do is to make your class A a static class

private static class A {
    // code here
}

so that you will be able to instantiate as

A a = new MainActivity.A("someString");

and the variables initialization as

private static final A[] aList;

static {
    a = new A[2];
    a[0] = new MainActivity.A("emails");
}

1 Comment

The problem solved by declaring my inner class A as static. Btw as the aList is defined inside MainActivity so it is ok to use A a = new A("emails");
0

final fields can only be initialized inline and in the constructor.

private static final A[] aList = new A[2];

After that you can use the static initializer

static {
 aList[0] = new A("emails");
}

or with a list

private static final List<A> aList = new ArrayList<>();

static {
 aList.add(new A("example"));
}

1 Comment

Looks like I should add constructor to MainActivity and place the initializer in it?

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.