You are on page 1of 7

=====================================================================================

===========

Salesforce Training By Mohan - VLR Training Institute ||| Contact: +91-6301332114

=====================================================================================
===========

Topic: Inner class

================================================================

* Inner class is helper/utility class, keeps code lean and clean

* Can't declare static methods and variables in inner classes

* If you write an apex class inside another apex class then that written class is called as inner class .

* We can use inner classes as wrapper classes in VF Pages to combine different datatypes.

* How many ways we can call innser classes

1. from main class

2. from outside the class

3. from other inner class of the same main class

================================================================

* Generic Syntax to access the inner class from any where:

ParentClassName.InnerClassName Variable = new ParentClassName.InnerClassName();

Ex-1)

================================================================

public class Oct30_MainCls {

public string stdName = 'John';

public integer batch ;


public Oct30_InnserCls in1 = new Oct30_InnserCls();

public Oct30_MainCls(){

batch = 4;

system.debug('Main class constructor===>' + batch);

public class Oct30_InnserCls{

public string studentNAme = 'Raj';

public string country ;

public integer batchNum;

public Oct30_InnserCls(){

country = 'India';

system.debug('inner class constructor===>' + country);

public void updateStdDetails(){

batchNum = 5;

system.debug('inner class Method===>' + batchNum);

_______________________________________________________________________

Oct30_MainCls oc = new Oct30_MainCls();


Ex-2)

================================================================

public class Oct30_MainCls {

public string stdName = 'John';

public integer batch ;

public Oct30_InnserCls in1 = new Oct30_InnserCls();

public Oct30_MainCls(){

batch = 4;

system.debug('Main class constructor===>' + batch);

in1.updateStdDetails();

public class Oct30_InnserCls{

public string studentNAme = 'Raj';

public string country ;

public integer batchNum;

public Oct30_InnserCls(){

country = 'India';

system.debug('inner class constructor===>' + country);

public void updateStdDetails(){

batchNum = 5;

system.debug('inner class Method===>' + batchNum);

}
_______________________________________________________________________

Oct30_MainCls oc = new Oct30_MainCls();

Ex-3) Calling Inner class from another apex class

================================================

public class Oct30_MainCls {

public string stdName = 'John';

public integer batch ;

public Oct30_InnserCls in1 = new Oct30_InnserCls();

public Oct30_MainCls(){

batch = 4;

system.debug('Main class constructor===>' + batch);

public class Oct30_InnserCls{

public string studentNAme = 'Raj';

public string country ;

public integer batchNum;

public Oct30_InnserCls(){

country = 'India';

system.debug('inner class constructor===>' + country);

public void updateStdDetails(){


batchNum = 5;

system.debug('inner class Method===>' + batchNum);

_______________________________________________________________________

public class Oct30_SecondCls {

public Oct30_SecondCls(){

//mainclassname.innerclassname variablename = new


mainclassname.innerclassname();

Oct30_MainCls.Oct30_InnserCls ins = new Oct30_MainCls.Oct30_InnserCls();

system.debug('ins before==>' + ins);

ins.country = 'India outside';

ins.batchNum = 45;

ins.updateStdDetails();

system.debug('ins after==>' + ins);

_______________________________________________________________________

Oct30_SecondCls oc2 = new Oct30_SecondCls();

Ex-5) Call main class from inner class

=======================================================

public class Oct30_MainCls {


public string stdName = 'John';

public integer batch ;

//public Oct30_InnserCls in1 = new Oct30_InnserCls();

public Oct30_MainCls(){

batch = 4;

system.debug('Main class constructor===>' + batch);

public class Oct30_InnserCls{

public string studentNAme = 'Raj';

public string country ;

public integer batchNum;

public Oct30_InnserCls(){

country = 'India';

system.debug('inner class constructor===>' + country);

Oct30_MainCls ocMain = new Oct30_MainCls();

ocMain.stdName = 'student name from inner class';

ocMain.batch = 60;

system.debug('main class calling from inner class===>' + ocMain);

public void updateStdDetails(){

batchNum = 5;

system.debug('inner class Method===>' + batchNum);

}
}

_______________________________________________________________________

Oct30_MainCls.Oct30_InnserCls oc = new Oct30_MainCls.Oct30_InnserCls();

oc.updateStdDetails();

=====================================================================================
===========

Salesforce Training By Mohan - VLR Training Institute ||| Contact: +91-6301332114

=====================================================================================
===========

You might also like