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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:03:20+00:00 2026-05-23T17:03:20+00:00

I have a best technique issue in Java. I have a base class implementing

  • 0

I have a best technique issue in Java. I have a base class implementing some genral methods. An a heap of inherited classes. Now I have to randomly select one of the inherited classes. My approach is the following:

  1. Having a array of the Class object for each inherited class
  2. Randomly generate an index for the array
  3. Create an instance of the class object at the random index from 2. with newInstance()

Code exmaple for step 1:

Class[] possibleClasses = {Class1.class, Class2.class}

Is this a sufficient approach? For example the Eclipse Indigo syntax correction says for the code example above “Class is a raw type. References to generic type Class should be parameterized”. So the definition without generic type may has side effects or so?

Stefan

  • 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-23T17:03:20+00:00Added an answer on May 23, 2026 at 5:03 pm

    Your approach is fine – indeed it’s probably the simplest and most effective approach.

    If you want to avoid the compiler warning you can add a generic type argument as follows:

    Class<?>[] possibleClasses = new Class[]{Class1.class, Class2.class}
    

    However this just gives you a different warning (unchecked conversion) so you might as well just suppress the original warning directly.

    A slightly more complex alternative approach would be to use reflection to walk the class hierarchy and automatically add all subclasses of your base class to an ArrayList of classes. The advantage is that you wouldn’t need to explicitly list out all the subclasses individually and it would automatically pick up any new subclasses added.

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

Sidebar

Related Questions

From what I have read best practice is to have classes based on an
What is the best/proper technique to share login between two sites. I have website
I have a best practice question. I'm developing a OO MVC framework in PHP,
does Android have a best practices guideline on creating & populating the db/tables programmatically
I know that primary keys based on Guids do not have the best performance
What best practices have you used in unit testing embedded software that are peculiar
Assuming that best practices have been followed when designing a new database, how does
The best that I have been able to come up with is: strlen(preg_replace('/^([\\*]*)\s(.+)/',$1,$line)); ^^That
The best one I have come up with so far is that a card
I have a question about best practices regarding how one should approach storing complex

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.