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

  • Home
  • SEARCH
  • 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 6927079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:00:06+00:00 2026-05-27T11:00:06+00:00

I have the class: class SomeClass<T extends SomeInterface>{ private T[] myArray; public SomeClass() {

  • 0

I have the class:

class SomeClass<T extends SomeInterface>{

   private T[] myArray;

   public SomeClass()
   {
       // I want to initialize myArray in here to a default size of 100
       myArray = new T[100];  // this gives an error
   }

}

I know I can fix that by requiring a parameter in the constructor as:

class SomeClass<T extends SomeInterface>{

   private T[] myArray;

   public SomeClass(Class<T> clazz)
   {       
       myArray= (T[]) Array.newInstance(clazz, 100);
   }

}

but it makes no scene having to pass the generic parameter twice.

in other words in order to instantiate an object from the class SomeClass I will have to do something like:

SomeClass<SomeOtherClass> obj = 
      new SomeClass<SomeOtherClass>(SomeOtherClass.class);

I program in c# and Java does not seem to be friendly. I don’t even understand why it is not possible to cast Object[] array to SomeOtherClass[] array. In c# that will be possible…

so my question is how can I avoid having to pass the SomeOtherClass.class parameter in order to be able to construct an array of the generic type in the constructor of the class…

  • 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-27T11:00:07+00:00Added an answer on May 27, 2026 at 11:00 am

    Yes, you would have to pass in the .class like that in order to make this work.

    You could avoid all of this and just use an ArrayList<T> instead. When you need it in the form of an array you can use: (T[]) myArrayList.toArray()

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

Sidebar

Related Questions

I have a class: public class SomeClass <TView extends View>{ private TView tv; public
Let's say I have some Java code: public class SomeClass { static { private
I have a class that looks like this public class SomeClass { public SomeChildClass[]
So I have a class with a method as follows: public class SomeClass {
Suppose I have a class that processes some data: class SomeClass { public: void
i have the codes below class ReservationController extends Zend_Controller_Action { public function init() {
I have a class like this class someclass{ public function somemethod(){} } Now I
I have this class package somePackage { public class SomeClass { public static const
I have a class like this: class SomeClass { public: QString data; SomeClass(const QString
I have a class similar to the following: class SomeClass { public: template<typename... Args>

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.