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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:48:35+00:00 2026-05-28T03:48:35+00:00

How do I declare an object of a variable type? I know that I

  • 0

How do I declare an object of a variable type? I know that I need to use generics, I’ve written this code but I’m not sure if it makes sense for what I want to do.
I want to declare an object of a variable type, and pass either an int or a string as a parameter for the object constructor.
Here is the code I wrote:

CityOp(String CityT, Class<?>[] par) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException{
    Class<?> co = Class.forName(CityT);
    Op.getDeclaredConstructor(par);
}

Does the code make sense?

  • 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-28T03:48:36+00:00Added an answer on May 28, 2026 at 3:48 am

    The question seemed a bit unclear to me. If you’re looking to pass in either a String or an int for the first parameter, and the second parameter must match the type of the first, then you can use a generic class to accomplish this, as follows:

    public class CityOp<T> {
        CityOp(T cityT, T[] par) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException{
            // Constructor body here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem is with object's variable: this.timer it's not global, so when I click
It's known that you should declare events that take as parameters (object sender, EventArgs
I see this all the time: object literals declared such that some keys are
I think I've declared a Vector with an object correctly. But, I don't know
This is not exactly a straight-out question because I have just solved it, but
Use the static modifier to declare a static member, which belongs to the type
Referring on this question , I have a similar -but not the same- problem..
I have a object named post and I declare a new variable named newPost
So I know that in Python variables declared in a method are not visible
I've found this pattern quite a few times in my code: if (doIt) object.callAMethod

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.