0

I'm trying to populate an array list of strings with the name of the constructors for a class. For some reason it's getting populated with the class name instead

Class<?> c = Class.forName(className);
        Constructor<?>[] construct = c.getDeclaredConstructors();
        for(int i = 0; i < construct.length; i++){
            memberList.add(construct[i].getName());
        }
7
  • 3
    Constructors are always named after the class they're in. Commented Mar 20, 2014 at 16:50
  • Because that is constructor name, you might be looking for signature Commented Mar 20, 2014 at 16:50
  • I know. But what is being added is the full path of the class(ie. java.util.Vector). Also it should populate with multiple constructors Commented Mar 20, 2014 at 16:51
  • What is the context of this code snippet? What are you trying to accomplish with this? Commented Mar 20, 2014 at 16:51
  • It's a JavaClass Viewer in Java EE. The array list populates a listbox with all class members Commented Mar 20, 2014 at 16:52

3 Answers 3

3

Constructors always take the name of the class - which is what you are seeing.

Sign up to request clarification or add additional context in comments.

5 Comments

What I'm seeing when I add it to the array list is the complete path of the class. Im also trying to return every constructor and only one item is being returned
This code looks to be inside a method - is that the case? It looks like the constructor array is being initialized each time and hence every time a call to the method is made, it is initializing the constructor array and resulting in just one constructor effectively being added to it. When you say complete path of the class - do you mean it return packagename+classname or fulldirectoryname+classname?
It is inside a method. Clicking a button will run this code once for every class you want to view. Example of what is being return is java.lang.Object, or java.util.Vector etc
If you have more than multiple constructors, can you post the getDeclaredConstructors() code? It seems to be returning only one constructor from your description.
Figured out a solution. In the api, getName supposedly return the constructor as a string. Tostring is woking fine though for now
0

You need to drop the ".getName()" if you want the Constructor object in the list instead of the name of the Constructor object.

1 Comment

It needs to be a string :/
0

Like Sunil said, the constructor has the same name as the class and you should make the difference between them checking his params.

To get the params you could try the next code:

    public static void main(String[] args) throws ClassNotFoundException {
    Class<?> c = Class.forName(Class1.class.getCanonicalName());
    Constructor<?>[] constructors = c.getDeclaredConstructors();
    ArrayList<String> memberList = new ArrayList<String>();
    for(Constructor<?> constructor:constructors){
        memberList.add(constructor.getName());
        System.out.println("--------------------------------------");
        System.out.println(String.format("constructor: %s",new Object[]{constructor}));

        for(Class<?> paramType: constructor.getParameterTypes()){
            System.out.println(String.format("constructor param type: %s",paramType));
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.