0

I'm studying the Visualforce developer documentation and saw this "You cannot create a custom controller constructor that includes parameters." on https://developer.salesforce.com/docs/atlas.en-us.232.0.pages.meta/pages/pages_controller_custom.htm

I just added the below code to the first Apex class example on that page :

public MyController(String s){
    
}

and it worked fine. Does it just mean only the constructor with no parameters is used or is there something else I'm not understanding?

1 Answer 1

1

A Visualforce Page whose controller attribute is set to your custom class will only call the no argument constructor. It has one by default, so if you don't define any constructor, the class can be used. If you define only a constructor that takes argument(s), then your implicit no-argument constructor is lost, and your page will not work any more. You can define both a constructor that takes argument(s) and one that takes none, but as stated in the docs, only the no-argument constructor will be used by the page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .