Create new object using reflection?

后端 未结 2 1113
梦毁少年i
梦毁少年i 2020-12-03 05:18

Given class Value :

public class Value {

    private int xVal1;
    private int xVal2; 
    private double pVal;


    // constructor of the Value class 

         


        
2条回答
  •  醉话见心
    2020-12-03 05:25

    You need to locate the exact constructor for this. Class.newInstance() can only be used to call the nullary constructor. So write

    final Value v = Value.class.getConstructor(
       int.class, int.class, double.class).newInstance(_xval1,_xval2,_pval);
    

提交回复
热议问题