Use the transient keyword to declare instance variables that can’t be saved, and shouldn’t be transmitted as part of the view state for a Visualforce page. For example:
Transient Integer calculateTotalOpportunityAmount;
Common use case: use transient keyword for field on a Visualforce page that is needed only for the duration of a page request, but should not be part of the page’s view state and would use too many system resources to be recomputed many times during a request. Example below:
transient public List connections { get; private set; }
transient public List previousEmployers { get; private set; }
transient public Set hashTags { get; private set; }
private List retrievePreviousEmployers() {
List a = new List();
a.add(new Account(Name = 'GE'));
a.add(new Account(Name = 'Siemens'));
a.add(new Account(Name = 'EADS'));
return a;
}
private List retrieveConnections() {
return [
SELECT Id, Name
FROM Contact
OFFSET 1
];
}
private Set retrieveHashTags() {
Set s = new Set();
s.add('#where');
s.add('#dreamforce');
s.add('#pto');
s.add('#highlights');
s.add('#bestpractice');
s.add('#protip');
s.add('#iwantapony');
s.add('#hr');
return s;
}