0

I have a litte problem with a static, direct filled array of class files, which inheriting from a superclass

 public static Class<SuperClass> classes= new Class<SuperClass>[]{
    ChildClass.class
}

seems to be impossible. Intellij says, it requires the Superclass.class, instead of ChildClass.class.

Why is this not possible? Thank you

1
  • 1
    You are assigning an array to a non-array variable. Commented Jun 28, 2013 at 14:16

3 Answers 3

1

Arrays and generics don't mix.

Also a type Xx<Derived> is not assignable to Xx<Base> (see bazillions of questions on this site).

You may want:

private static final Class<? extends SuperClass> clazz = ChildClass.class;

The other way around:

private static final Class<? super ChildClass> clazz = SuperClass.class;

Or use an appropriate collection:

private static final Set<? extends SuperClass> classes =
    unmodifiableSet(singleton(
        ChildClass.class
    ));

Mutable statics, even if not public, are a really bad idea.

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

Comments

0

It is not possible because we use =(equal) to say left and right hand sides are same.. here you are assigning an array to a non-array variable.

Comments

0
  1. There are no generic arrays. You cannot create an array of Class
  2. Class is not a subclass of Class

1 Comment

Thank you. The reason why i need this is the following: I have several filters and I need to define, which filter classes my program is allowed to use. maybe I should just put them as objects into an array on startup or something...

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.