Wrapper Class

Wrapper Class in Apex Salesforce: A wrapper or container class is a class, a data structure which contains different objects or collection of objects as its members.
A wrapper class is a custom object defined by a programmer wherein he defines the wrapper class properties. Consider a custom object in salesforce, what do you have in it? fields right? different fields of different data types. Similarly wrapper class is a custom class that has different data types or properties as per requirement. We can wrap different object types or any other types in a wrapper class.
Wrapper Class in Apex Salesforce : A wrapper or container class is a class, a data structure which contains different objects or collection of objects as its members.
A wrapper class is a custom object defined by a programmer wherein he defines the wrapper class properties. Consider a custom object in salesforce, what do you have in it? fields right? different fields of different data types. Similarly wrapper class is a custom class that has different data types or properties as per requirement. We can wrap different object types or any other types in a wrapper class.
Below example, we have created our own list with two different data types.
public class wrapper {
/* Simple data type example public void callwrapper(){
List<wrapperclass> lstwrapeer=new List<wrapperclass>();
wrapperclass wrapperObj=new wrapperclass();
wrapperObj.x=10;
wrapperObj.y='ranbir need Rs.';
lstwrapeer.add(wrapperObj);
system.debug(lstwrapeer);
}
public class wrapperclass{
public integer x;
public string y;
}
*/
public void callwrapper(){
List<wrapperclass> lst=new List<wrapperclass>();
//Account acc=[select id,name from Account];
contact cnn=[select id,firstname from contact limit 1];
for(Account acc_new:[select id,name from Account]){
wrapperclass obj=new wrapperclass(acc_new);
lst.add(obj);
}
wrapperclass obj1=new wrapperclass(cnn);
lst.add(obj1);
system.debug(lst.size());
}
public class wrapperclass{
public string name;
public wrapperclass(Account x1){
name=x1.name;
}
public wrapperclass(contact x1){
name=x1.firstname;
}
}
}