0

Getting a Generic array creation error on a non generic array, I think?

So, this code works fine, pretty straight-forward.

public class test {
  private subTest[] subTests;
  private class subTest {

  }

  public test(int size) {
    subTests = new subTest[size];
  }
}

But what I am actually trying to do is something more like this:

public class test<T> {
  private subTest[] subTests;
  private T[] arrayOfGenerics; 

  private class subTest {

  }

  public test(int size, int size2) {
    arrayOfGenerics = (T[]) new Object[size];   //This line works fine
    subTests = new subTest[size2];              //This line is the one giving me a 'Generic array creation' error
  }
}

1 Answer 1

3

subTest is an inner class of test, so the type parameter of test is visible from within subTest. Therefore, you are trying to create an array of generic type

test<T>.subTest[]

You can solve this by making the class subTest a static nested class instead:

private static class subTest

If you do this, the type of the array is just

test.subTest[]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.