Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Session 2:

Setter and getter methods :

Setter method : This method is used to read the value from the Vf page and store
the value
to a variable in apex class

Ex : Integer age ;

public void setAge(Integer age){


this.age=age;
}

Ex: String name;

public void setName(String name){

this.name=name;
}

Getter Method : This method is used to read the value from the apex class and
return the

value to the VF page;

Ex: Integer age ;

public Integer getAge(){


return age;
}

Ex: String name ;

public String getName(){


return name;

===>Fetching the data from class and displaying in VF page

1. Apex class which we want to refer in the page should be declared as


controller in the VF page

Ex: <apex:page controller="ApexclassName" >

2. when ever we give an expression {! var } it call the getter method of the
var

Ex: {! name} it call getName() method from the apex class

Example :

Apex class:

public class GetterExample {


public String branch;
public String getBranch(){
return branch;
}
public String getName(){
String myname='Testing';
return myname;
}
public Integer getAge(){
return 10;
}
public GetterExample(){
branch='SRNagar';
}
}
VF Page:

<apex:page controller="GetterExample">
Name : {! name } <br/>
Age : {! age } <br/>
Branch: {!branch}
</apex:page>

===> In case if we want to simply return the values that are stored in the class
variables then
use standard format of getter and setter

DataType variable {set;get;}

Ex: Integer age {set;get;}


String name {set;get;}

Example :
Apex Class Name :GetsetExample

public class GetSetExample {


public Integer age {set;get;}
public String name {set;get;}
public GetSetExample(){
age=20;
name='Prasad';
}

}
VF Page:
<apex:page controller="GetSetExample">
Name : {!name} <br/>
Age : {!age}
</apex:page>
===================================================================================
===========

Scenario 1: Create a VF page with section Header where the


title ,subtitle ,description values
are fetched from apex class

ApexClass :SectionHeaderExample

public class SectionHeaderExample {


public String title {set;get;}
public String subTitle {set;get;}
public String description {set;get;}
public SectionHeaderExample(){
title='Apex Class';
subTitle='Gettter and Setter';
description='This is an example to demonstrate getter and setter';
}
}

VF Page:
<apex:page controller="SectionHeaderExample">
<apex:sectionHeader title="{!title}" subtitle="{!subTitle}" description="{!
description}" />
</apex:page>

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

You might also like