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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:32:21+00:00 2026-05-26T14:32:21+00:00

Is this a safe way to create an instance (assuming T has a accessible

  • 0

Is this a safe way to create an instance (assuming T has a accessible default constructor) ?

<T>
public T defaultObj(T obj) throws Exception {
    return (T) obj.getClass().newInstance();
}

Because of the type erasure, the above codes will generate an unchecked warning. Is it possible to get rid of this warning other than @SuppressWarnings?

Many thanks in advance!

Edit:
Yes, I know if I could pass a class<T> type which would be much better. But let’s just assume it is out of question for now.

  • 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-26T14:32:21+00:00Added an answer on May 26, 2026 at 2:32 pm

    That’s exactly what @SuppressWarnings is supposed to do: this code is safe (because calling getClass() on an object of type T actually yields Class<? extends T>), but compiler cannot understand it (return value of getClass() is declared as Class<?> due to limitations of generics) and generates warning.

    It’s absolutely legal to use @SuppressWarnings in this case, and you shouldn’t try to avoid using it.

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

Sidebar

Related Questions

Is this thread safe? private static bool close_thread_running = false; public static void StartBrowserCleaning()
In other words, is this Singleton implementation thread safe: public class Singleton { private
Is it safe to do something like this in Node.js/Express.js? // use Object.create(null) so
Is this safe to do? Does fgets terminate the buffer with null or should
my project this year : build safe search engine for kids so i need
Under what circumstances would this or would this not be safe? I have a
I'm seeing this code in a project and I wonder if it is safe
I cant believe this, i always throught the below would be concurrency safe. I
I am looking into creating type-safe generic controls. This is targeting the (reduced) generics
I just want to confirm what I've understood about Generics in C#. This has

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.