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

The Archive Base Latest Questions

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

I am using the getConstructors() method to pull the class’s contructors. The class is

  • 0

I am using the getConstructors() method to pull the class’s contructors. The class is in an abstract superclass reference, and I won’t know which subclass is being called until the user decides. Here’s what I have so far.

Weapon stickCopy = stick;
System.out.println(stick);
System.out.println(stickCopy);

Class <? extends Weapon> myClass = stick.getClass( );

System.out.println(myClass.getSimpleName( ));

Constructor<?>[] construct = myClass.getConstructors( );

for(Constructor<?> constructor: construct)
{
    System.out.println(constructor);
}

try
{
    stickCopy = (Weapon) construct[2].newInstance((stick));
}
catch (Exception e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

System.out.println(stick);
System.out.println(stickCopy);

The Stick class is a subclass of the abstract Weapon class. I am trying to figure out the code for a deep copy from a Weapon reference, stored in a player object. Since Weapon is abstract, I can’t call a copy constructor from it. In my Stick class, the copy constructor is the third constructor, and so I hard-coded 2 into the construct array in the try statement. If I change the stick class’s constructors by, say, adding a new constructor in front of the copy constructor or reordering them, how can I find the position of the copy constructor at run-time?

Also, I have never used Generics before, so, if I am not following what are generally considered good programming practices, please correct me.

  • 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-31T14:18:19+00:00Added an answer on May 31, 2026 at 2:18 pm

    If you use the version of getConstructor that takes arguments you can specify the constructor you want by its argument types. Presumably you’ll want to pass either Stick.class or Weapon.class in:

    Constructor constructor = myClass.getConstructor(myClass);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that allows, using an abstract class, people to write their
I'm trying to instantiate a class using the Constructor.newInstance() method but am running into
Using C#, I need a class called User that has a username, password, active
In order to avoid Java exceptions I'm using Scala's exception handling class. However, when
I am trying to create new instances of a class (specifically a subclass of
I'm using Type.GetConstructor(Type.EmptyTypes) to get the default constructor for a class. It works if
We are using JBoss 4.2.3 which in turn comes with version 3.2.1.ga of Hibernate.
Is it possible to see which constructor was the generic one? internal class Foo<T>
I am using reflection to construct a class (ConfigBuilder) that takes a File as
I am using an abstract factory to return instances of concrete subclasses.I would like

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.