LearnFrenzy
Sign in Take a free test

Generics · Find the Error · Question 6

Error: creating an array of a type parameter Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Buffer<T> {
    private final T[] slots = new T[10];
    T get(int i) { return slots[i]; }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(new Buffer<String>().get(0));
    }
}
  • A 10 is too small
  • B Arrays need a known run-time element type, but T is erased; use a List<T>, or (T[]) new Object[10] with care Correct
  • C get must be static
  • D slots must not be final
Correct Answer: Option B

javac: "generic array creation".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links