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
Shreya SalesforceShreya Salesforce 

Why we declare Variables outside the controller in Apex...What is the use of it?

public with sharing class Account_manager{
    public Contact placeholderContact{get;set;}
    public List<Contact> accountContacts{get;set;}
    
    public Account_manager(){
    placeholderContact= new Contact();
    accountContacts= new List<Contact>();
    }
}
Vishal_GuptaVishal_Gupta
Hi Shreya,

Do you mean outside the construstor? if yes then to use those public variable values in your logic in any method or to bind the values of those variables in your VF page.

Please let me know if you got your answer.
 
Deepali KulshresthaDeepali Kulshrestha
Hi Shreya,

The reason to declare Variables outside the controller in Apex is to make variable accessible in the Vf Page.
If you want to make variable accessible to Vf page you have to make it public and must declare at the class level.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha