samedi 22 avril 2017

How to create dynamic object for class in java?

Ex:

Element elementInclude1 = doc.createElement("include");
elementMethods.appendChild(elementInclude1);
elementInclude1.setAttribute("name", "T1");

Element elementInclude2 = doc.createElement("include");
elementMethods.appendChild(elementInclude2);
elementInclude2.setAttribute("name", "T2");

Element elementInclude3 = doc.createElement("include");
elementMethods.appendChild(elementInclude3);
elementInclude3.setAttribute("name", "T3");
....

In the above example, number of objects(elementInclude1, elementInclude2,..) created are known dynamically.

No of object required are known in run time, using that value iterating in the loop new object created in each iteration. how can I achieve that.

Need to create objects elementInclude1, elementInclude2, elementInclude3,... dynamically while run time

Just for better understanding I used some wrong practice in the below code.

String noOfObj = 5;
for(int i = 1; i<=noOfObj; i++)
{
Element elementInclude+**noOfObj** = doc.createElement("include");
elementMethods.appendChild(elementInclude+**noOfObj**);
elementInclude+**noOfObj**.setAttribute("name", "T1");
}





Aucun commentaire:

Enregistrer un commentaire