+ 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>();
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