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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:56:18+00:00 2026-05-31T04:56:18+00:00

Class A is of generic type and in class B i want to create

  • 0

Class A is of generic type and in class B i want to create an array of objects of type A with Integer as the generic parameter.

class A<T>
{}

class B
{
    A<Integer>[] arr=new A[4]; //statement-1
    B()
    {
         for(int i=0;i<arr.length;i++)
              arr[i]=new A<Integer>();
    }
}

But in statement-1 i get a warning for unchecked conversion. What is the correct way of creating this array so i don’t need to use any suppress warnings statement.

  • 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-31T04:56:19+00:00Added an answer on May 31, 2026 at 4:56 am

    Sometimes Java generics just doesn’t let you do what you want to, and you need to effectively tell the compiler that what you’re doing really will be legal at execution time.
    So SuppressWarning annotation is used to suppress compiler warnings for the annotated element. Specifically, the unchecked category allows suppression of compiler warnings generated as a result of unchecked type casts.
    Check this..

    @SuppressWarnings("unchecked")
    A<Integer>[] arr = new A[3];
    B(){
       for(int i=0;i<arr.length;i++)
       arr[i]=new A<Integer>();
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a generic class that takes a type parameter and restrict
Is there a way to create a generic class from type parameter. I have
I need to write a generic class where the type parameter must be something
I want to create a extension class for the List type so users can
I need to create an extension method to array class, but this extension method
i want to create a generic class that would accept T. T is an
I have a Class with generic type: class Foo<T> { protected boolean validateType(Object obj){
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a Base class that needs a Generic type. That can be either
There is any way to set the generic type (T) of class in the

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.