There are two solutions that do not require passing in the class object. In both I am assuming that since it's a private variable, you are the only person putting things into it, and the outside world doesn't see it.
One is to just use a plain Object array Object[]
. The disadvantage is that you will lose the benefits of generics and you will have to cast things coming out of it, which makes the code uglier. But since you are the only person putting things into it, then the cast should always be safe. The outside doesn't need to know what's going on.
The other options is kind of a hack. You create an Object[]
, and then "cast" it to T[]
. Technically this is illegal, because Object[]
and T[]
are different runtime types and the former cannot be cast to the latter. But as long as you don't pass the array out of the class (e.g. return it or something), then it won't cause any problems, because T[]
will get erased to Object[]
anyway, and no cast is actually performed. Then you can get the benefits of generics in your class for easier coding, but you have to be extremely careful not to pass that array to anyone who actually expects a T[]
, because it is not.