The below example will help you to understand the way of assigning and accessing outer class and inner class variables.
Example Code:
This PersonalInfo class contains Name, Address and Others inner classes for easiest way of access.
Outer class contains the below two variables instances,
How to assign and access values for PersonalInformation class(Including inner classes)?
Example Code:
- public class PersonalInformation {
- public String studentRollNo;
- public PersonalInfo personalDetails;
- public class PersonalInfo{
- public Name nameInfo;
- public Address addressInfo;
- public Others otherInfo;
- }
- public class Name{
- public String firstName;
- public String lastName;
- }
- public class Address{
- public String addressLine1;
- public String addressLine2;
- public String addressLine3;
- }
- public class Others{
- public Integer age;
- public String nationality;
- }
- }
- PersonalInformation - Outer Class
- Name - Inner Class
- Address - Inner Class
- Others - Inner Class
- PersonalInfo - Inner Class(Act as a Container class)
This PersonalInfo class contains Name, Address and Others inner classes for easiest way of access.
Outer class contains the below two variables instances,
- studentRollNo - String
- personalDetails - PersonalInfo class instance to access all inner classes.
How to assign and access values for PersonalInformation class(Including inner classes)?
- ////////////////////////// Assigning Values //////////////////////////////////////////
- //Initialize Outer Class.
- PersonalInformation personalInfo = new PersonalInformation();
- //Fill Name class details
- PersonalInformation.Name nameCls =new PersonalInformation.Name();
- nameCls.firstName = 'Arunkumar';
- nameCls.lastName = 'R';
- //Fill Address class details
- PersonalInformation.Address addressCls =new PersonalInformation.Address();
- addressCls.addressLine1 = 'Address 1';
- addressCls.addressLine2 = 'Address 2';
- addressCls.addressLine3 = 'Address 3';
- //Fill Others class details
- PersonalInformation.Others othersCls =new PersonalInformation.Others();
- othersCls.age = 25;
- othersCls.nationality = 'Indian';
- //Initialize the container class
- PersonalInformation.PersonalInfo personalInf = new PersonalInformation.PersonalInfo();
- personalInf.nameInfo = nameCls;
- personalInf.addressInfo = addressCls;
- personalInf.otherInfo = othersCls;
- //Assign values to the Outer calss variables.
- personalInfo.studentRollNo = 'LE30088';
- personalInfo.personalDetails = personalInf;
- System.debug('## Personal Info Class '+personalInfo);
- ////////////////////////// Accessing Values //////////////////////////////////////////
- PersonalInformation personalInfoCls = personalInfo;
- // Accessing parent class Student Roll Number Value
- System.debug('Student Roll Number: '+personalInfoCls.studentRollNo);
- // Accessing Name inner class details from the container class instance.
- PersonalInformation.Name nameDetails = personalInfoCls.personalDetails.nameInfo;
- System.debug('Name Class: '+nameDetails);
- System.debug('First Name: '+nameDetails.firstName);
- System.debug('Last Name: '+nameDetails.lastName);
- // Accessing Address inner class details from the container class instance.
- PersonalInformation.Address addressDetails = personalInfoCls.personalDetails.addressInfo;
- System.debug('Address Class: '+addressDetails);
- System.debug('Address Line 1: '+addressDetails.addressLine1);
- // Accessing Others inner class details from the container class instance.
- PersonalInformation.Others otherDetails = personalInfoCls.personalDetails.otherInfo;
- System.debug('Others Class: '+otherDetails);
- System.debug('Age: '+otherDetails.age);