Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:25:55+00:00 2026-06-14T19:25:55+00:00

Let say I want to create an array of length 5 through the default

  • 0

Let say I want to create an array of length 5 through the default constructor

public class A <E extends Comparable<? super E>> implements B<E>
{
     private E[] myArray;


     public A()
     {
         myArray = (E[]) new Object[5]; 
     }    

}

Is this the right way of doing it? I’m confused that whether I have to
state “Comparable” before []. so

 private  Comparable[] myArray;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:25:56+00:00Added an answer on June 14, 2026 at 7:25 pm

    The problem with the code you posted is that the erasure of E[] is Comparable[] (because E‘s upper bound is Comparable), so casting an object whose real type is Object[] to Comparable[] will fail.

    You could simply change it to the following and it won’t throw the exception:

    public class A <E extends Comparable<? super E>> implements B<E>
    {
         private E[] myArray;
    
    
         public A()
         {
             myArray = (E[]) new Comparable[5]; 
         }    
    
    }
    

    However, there is more subtlety here: myArray is not really of type E[]. Inside the class, E[] is erased to Comparable[], so it is okay. But you have to make sure never to expose myArray to outside of the class. Since it is private, you just have to make sure that no method returns it or something.

    The benefit of doing it this way, keeping myArray of type E[], over Jatin’s answer of using Comparable[], is that you don’t have to cast every time you get something out of it, so the code is nicer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I want to create a collection class that is thread-safe by default.
I want to create an object, let's say a Pie. class Pie def initialize(name,
Let's say I create an instance of a class and want to assign some
Let's say I have an array : char[] chars={'X','X','m','a','t','t','X','X'}; I want to create String
I want to create a new array. Let's say int[] clickNum = new int[800];
Let's say I have created two objects from class foo and now want to
Let's say you have a 3d array A[I][J][K], but you want to permute it
Let's say I want to create an 100x100 matrix of which every row contains
Let's say before my webapp starts, I want to create all dom elements initially
Let's say I have the following class. MyClass<T> { public void MyMethod(T a, List<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.