+ Start a Discussion
Kumar Rao 1Kumar Rao 1 

Why is Constructor in a Controller

Dear Folks,

I'm very new to Salesforce and VisualForce (of course Programming), Can some one please educate me: 

1. Why Constrcutor is required for a VisualForce Page?
2. What is the actual need of this?
3. What would happen If a constructor is not used inthe Controller?

Here is a sample code.. I understood the logic but I don't understand wht we need AcExt Constructor?

Public with sharing class AcExt {     

Public Account ac;     

Public AcExt(ApexPages.StandardController stdctrl){     
     ac = (Account)stdctrl.getRecord();     
}          

Public string GetGreeting(){     
return 'Your ID is ' +ac.id;     
}     

}

Thanks,
Kumar
Beau Gordon 14Beau Gordon 14
A visualforce page doesn't require a controller.  However, if it uses a controller, the controller needs a constructor.  

The controller is a class.  The constructor tells us how to create an instance of that class, and the VF page uses that instance to know what information to display.