0

Trying to create this:

class Widget
{
  public Widget(Object type)
  {
    Object typeCheck = type.getClass();
    (typeCheck class)[] = new (typeCheck class)[0];
  {
}

Where getting the type of typeCheck and using that to create an array is the troublesome bit. Is there an actual logical way to do this? Or should I just parse the result of getClass() and do it that way?

4

1 Answer 1

0

Theres a creator in Array class that accepts Class as argument and returns an Object that is a array.

Object typeCheck = type.getClass();
Object[] = (Object[]) new Array.newInstance(typeCheck, 0);

But you cant cast to the generified type captured from getClass() you have to use Object.

So you have three options.

1) Use the properly type if you know all methods

2) Use reflection all the way down

3) Use Generics with a interface, that also will give you compile time methods to call:

class Widget<T extends Component> {
    public Widget(T type) {
      T[] ts = new T[0];
    }
}
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.