0% found this document useful (0 votes)
9 views4 pages

Initialization Blocks and Inner Class

Apex Initialization Blocks And Inner Class

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Initialization Blocks and Inner Class

Apex Initialization Blocks And Inner Class

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Initialization Blocks:

Blank initialization blocks:


 Why we need these as already we have constructors to initialize variables?
With constructors we can take user input and set it to class variables then initialize the
values, but with the blocks we can’t do.
 These are not shared with all the instances.
 We can have multiple initialization blocks

Static initialization blocks:


 These are shared with all the instances (one copy)
 Can’t take user inputs
 Static Initialization blocks called first when all blocks are present

Order of executing the blocks:


1. Static Initialization block
2. Blank Initialization block
3. Constructors

public class Blocks {

//Initialization block 1

System.debug('Initialization block 1 is called');

//Initialization block 2

System.debug('Initialization block 2 is called');

//static Initialization block 1

Static {

System.debug('Static Initialization block 1 is called');

//static Initialization block 2


Static {

System.debug('Static Initialization block 2 is called');

//blank constructor

public Blocks(){

System.debug('Blank constructor is called');

//Parameterized constructor to set class value

public Blocks(Integer recoveredInArea){

this();

System.debug('Parameterized constructor is called');

}
Inner Class:
 Inner class is used to logically group the classes together
 When a class is supposed to be used by one single class

public class Company {


public String companyName;
public String ceo;
public Integer employeeCount;
public Long revenue;

// List of all customers

// add new customer

// print the list of all customers

// private inner class to store customer information


}

/** * Company Class Stores information about the company and its customers * */

public class Company {


public String companyName;
public String ceo;
public Integer employeeCount;
public Long revenue;

// List of all customers


private List<Client> customers = new List<Client>();

// add new customer


public void addNewCustomer(String name, String website, String email, Long phone) {
Client customer = new Client(name, website, email, phone);
customers.add(customer);
}

// print the list of all customers


public void getAllCustomers(){
for(Client customer : customers){
System.debug('Customer Name: '+customer.clientName+', Website: '+customer.website+',
Phone: '+customer.phone+', Email: '+customer.email);
}
}

// private inner class to store customer information


private class Client {
public String clientName;
public String website;
public String email;
public Long phone;

Client (String clientName, String website, String email, Long phone){


this.clientName = clientName;
this.website = website;
this.email = email;
this.phone = phone;
}
}
}

------------------------------------- dev console ------------------------------


Company companyeg = new Company();
companyeg.companyName = 'Google Academy';
companyeg.ceo = 'Sundar P';
companyeg.employeeCount = 6;
companyeg.revenue = 1000000;

companyeg.addNewCustomer('ABC Infotech', 'abcinfotech.com', '[email protected]',


7778889990L);
companyeg.addNewCustomer('Foodpanda', 'foodpanda.com', '[email protected]',
7778889990L);
companyeg.addNewCustomer('XYZ Infotech', 'xyzinfotech.com', '[email protected]',
6668889990L);

companyeg.getAllCustomers();

You might also like