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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:47:40+00:00 2026-06-01T10:47:40+00:00

Please forgive my ignorance, but I wasn’t able to find an answer elsewhere. I

  • 0

Please forgive my ignorance, but I wasn’t able to find an answer elsewhere.

I have the following code:

int value = 5;
Strategy s = new Strategy(convert(value));
mainClass.setStuff(s, value);

The Strategy is initialized with a number that is based on a conversion of “value”.
The mainClass uses the unconverted “value”.

What I would like to do is to simplify this by giving mainClass only the information which Strategy to use and with which unconverted value, whereupon mainClass should do the instantiation and initialization of the Strategy and process the value.

In C++ this would be possible with a template:

int value = 5;
mainClass.setStuff<Strategy>(value);

Can I make this work in Java somehow? It doesn’t seem to work with generics as unlike C++, Java cannot infer the type of the argument, so I can’t initialize Strategy.

  • 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-06-01T10:47:42+00:00Added an answer on June 1, 2026 at 10:47 am

    Change the setStuff method signature to public <T extends Strategy> ReturnType setStuff(Class<T> clazz, int value). Then you can find the correct constructor and create your new instance.

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

Sidebar

Related Questions

Please forgive the ignorance, but I'm very new to PHP and I couldn't find
Please forgive my ignorance, but I am noob. I have 4 Macs in my
This is probably a very simple question so please forgive my ignorance, but can
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
Please forgive my ignorance and poor SQL programming skills but I am normally a
Still new to LINQ2SQL so please forgive my ignorance ... I have one user
Please forgive my all-encompassing ignorance, but I just can't get my table to look
I am relatively new to columnar database, please forgive ignorance. Lets say I have
I am a dojo newbie so please forgive my ignorance. I have a dojox.mobile.EdgeToEdgeDataList
I'm just starting with OO php so please forgive my ignorance. Assuming I have

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.