function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc freshersfdc fresher 

what is the use of writting the code in constructor and use that class in vf pages

hi folks, 
could any one please explain me :
when we write the code in the constructor, once we create the object automatically consturctor called.
But what is the use if we use that class in Visualforce page as controller, we dont create any object there right?
 what if we write the code in constructor. Could anyone please explain me.
I see below code ,what happens when we write this.
 public     AccountAndContacts()
    {
        accs=new Account();
        cts=new list<Contact>();
        add();
    }

Thanks...
sfdcMonkey.comsfdcMonkey.com
typically Constructors are used to instantiate variables and members.  When the visualforce page is loaded the constructor is hit and then logic in the constructor is run.. (the getter setters are run first before the constructors... )

and in your above code we just instantiate our variables on page load.
Thanks , let us know if it helps you
 
Narender Singh(Nads)Narender Singh(Nads)
-"But what is the use if we use that class in Visualforce page as controller, we dont create any object there right?"

You are right that we don't create an object of the controller class on VF page explictly but what happens is an object is implictly created whenever that VF page is rendered.