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 7632499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:38:29+00:00 2026-05-31T06:38:29+00:00

I am fully aware that generic arrays cannot be instantiated like such: data =

  • 0

I am fully aware that generic arrays cannot be instantiated like such:

data = new Entry<K, V>[];

This will result in an error:

Cannot create a generic array of Entry<K,V>

So, why am I allowed to declare an instance variable that is a generic type array with no errors?

private Entry<K, V>[] data;
  • 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-05-31T06:38:30+00:00Added an answer on May 31, 2026 at 6:38 am

    In principle, the comment of Joachim Sauer is already your answer, however, I would like to detail it a bit.

    Sun (Oracle) knows of a phenomenon called memory-pollution, which always happens if a generic variable pointer points to a type-incompatible object. This can be enforced for example with the following code:

    List<String> list = new ArrayList<String>();
    List<Number> numberList = (List<Number>)(List)list;
    

    obviously, you will start seeing ClassCastExceptions, once you start working with that code. This was perfectly ok for Sun when designing Generics, because you get an obligatory warning RawType/Unchecked conversion. Whenever this warning is issued, you know, that you have code, which is not 100% type-checked and memory pollution may occur.

    The overall design principle in Generics is, that all possible memory-pollutions are indicated by such warnings. This is why the creation of generic arrays is forbidden. Assuming it was not, here is what could happen:

    List<String>[] array = new List<String>[5];
    Object[] oArray = array // this works without warning and has to for compatibility
    List<Object> oList = new ArrayList<Object>();
    oArray[1] = oList;
    

    You will have memory-pollution there without any warning and for compatibility reasons, a warning cannot be generated. This is why Sun decided to forbid arrays of generic types. However variables might be declared because you get your unchecked conversion warning there and this is all Sun wanted: a warning if pollution could occur.

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

Sidebar

Related Questions

I'm fully aware that this question has been asked and answered everywhere, both on
I'm fully aware that set division can be accomplished through a series of other
Disclaimer Yes, I am fully aware that what I am asking about is totally
Being resonably new to using GTK+, im not fully aware of all its functionality.
We have a Service that continuously collects sensor data on the phone. This service
Edit: I'm well aware of that this works very well with value types, my
Fully aware of the LaTeX features to define new environment allowing me to do
I am aware that System.Threading.Timer exists, but I already have a Thread. This thread
We have a problem with deserializing an exception. I am fully aware that it's
I'm fully aware that the return value of std::type_info::name() is implementation-defined. From the C++

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.