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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:16:13+00:00 2026-05-23T02:16:13+00:00

Sorry I couldnt think of a more concise title. My question is why does

  • 0

Sorry I couldnt think of a more concise title.

My question is why does the following piece of code work:

public abstract class TObjectPool<T> {
protected Object[] availableObjects;

TObjectPool(int size){
    availableObjects = new Object[size];
}

protected class RenderElementPool extends TObjectPool<RenderElement>{
    @Override
    public void fill() {
        for (int i = 0; i < capacity; i++) {
            availableObjects[i] = new RenderElement();   
        }
    }
}

when it will not work making the Object array a generic as so:

public abstract class TObjectPool<T> {
protected T[] availableObjects;

TObjectPool(int size){
    availableObjects = (T[]) new Object[size];
}

When the availableObjects[i] = new RenderElement(); line is executed in this latter example I get a ClassCastException. I understand why it works in the first example but not why it doesn’t in the second. availableObjects should be an array of RenderElement and I am trying to give it a RenderElement. What crucial bit of info am I missing?

Thanks for the help.

Update…

Thanks very much for the responses. I thought I understood but I have managed to confuse myself again:

If I add the function:

public void add(int index, T object){
    availableObjects[index] = object;
}

to the TObjectPool class. It will happily work with the T[] array.

So the new TObjectPool and subclassed pool are as follows:

public abstract class TObjectPool<T> {
T[] availableObjects;

TObjectPool(int size){
    availableObjects = (T[])new Object[size];
    capacity = size;
    count = capacity;
    fill();
}

public void add(int index, T object){
    availableObjects[index] = object;
}

protected class RenderElementPool extends TObjectPool<RenderElement>{
@Override
public void fill() {
    for (int i = 0; i < capacity; i++) {
        add(i, new RenderElement()); //this works
        //availableObjects[i] = new RenderElement(); //this gives an exception          
    }
}
}

I know how I can get round this now after reading your responses but I am curious. Can anyone shed some light into this peculiarity?

Thanks again.

  • 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-23T02:16:14+00:00Added an answer on May 23, 2026 at 2:16 am

    The cast fails because Object[] is a super class of T[] for all T, but not a subtype for any T other than Object.

    If you have a Class<T> available at runtime, you can use Array.newInstance to create a T[].

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

Sidebar

Related Questions

Sorry about the bad title, I couldn't think of a more descriptive one. The
A question for those familiar with MigLayout sorry couldn't think of a more appropriate
Sorry for dodgy title, couldn't think of anything better... Anyway, I have written a
Sorry about the title, I couldn't think of a better way to describe the
Sorry, poorly descripted title and couldn't think of how to phrase it. Working on
Sorry for the awful title, I couldn't think of anything better. Basically I have
Sorry if the title isn't very clear, I couldn't think of anything better... I'm
Why won't this code work correctly? I am sorry I couldn't understand how to
Sorry for the rather vague title, I couldn't think up a better description for
Sorry for the bad title (couldn't think of a better way to describe it)

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.