I thought these two samples would be functionally equivalent. But they behave differently:
Initializing a property in the constructor:
public with sharing class LeadController { public ApexPages.StandardController List {get; set;} public LeadController() { this.List = new ApexPages.StandardController(new Lead()); } }
(Works as expected. Super easy to manipulate the Lead from the Visualforce Page.)
Initializing a property in the accessor:
public with sharing class LeadController { public ApexPages.StandardController List { get {return List != null ? List : new ApexPages.StandardController(new Lead());} set; } }
(Does not work. The state of the Lead goes missing in between form submissions.)
From a style point of view,the second example is preferable because the property is dealt from in exactly one place, as opposed to "cross cutting" into the constructor. But it misbehaves.
Can anyone explain why?