I have written controller like below with some overloaded constructors –
public with sharing class TestController {
public TestController() { }
public TestController(ApexPages.StandardSetController controller)
{
pageInit();
}
public TestController(ApexPages.StandardController stdController)
{
pageInit();
}
}
Page code is something like this –
<apex:page standardController="Opportunity"
recordSetVar="Opportunity" title="Select Opportunity"
extensions="TestController" wizard="false" sidebar="false" showHeader="false"
tabStyle="Opportunity" readOnly="false" cache="false">
</apex:page>
1) How to identify which constructor will be called at the time of page load when overloaded constructors are there?
2) In the above scenario is there any way to call default constructor at the time of page load?
Best Answer
Which constructor will be called for an extension?
The constructor for an extension used in the Apex is determined by the VisualForce page, based on the
recordsetVar
attribute.If
recordsetVar
is defined in the apex:page element, the platform will try to inject anApexPages.StandardSetController
and will fail if there is no constructor that takes one as parameter.If
recordsetVar
is not set, the platform will try to inject anApexPages.StandardController
.Can the default constructor be called?
Yes, by defining your VF page to load the class as a controller, not an extension. If the page is defined with
standardController="Opportunity" extensions="MyClass"
then MyClass will be instantiated as a controller extension, with a standard controller or standard set controller injected into it, as above. If the page is defined ascontroller="MyClass"
then MyClass will be instantiated as controller in its own right, with nothing injected, using the default constructor.