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
Shiv ShankarShiv Shankar 

Difference between Getter & setter methods and property ??

Hello friends,

 

I have confusion between two things Getter & setter methods and property...........are they same thing or different thing . please write comparison between them.

 

For example - i want to display opportunity against an account in a visual force page.

 

My First Approach : 

public class PrintOpp{
    List<opportunity> oppList;
    
    public List<opportunity> getoppList(){
        if(oppList == null){
            oppList = new List<opportunity>();
            oppList = [select Name,Amount,CloseDate From Opportunity where AccountId =: act.id]; // suppose act is holding account record.
        }    
        return oppList;
    }
}

 another approach :

  

public class PrintOpp{
    public List<opportunity> oppList{
        get{
            if(oppList == null){
                oppList = new List<opportunity>();
                oppList = [select Name,Amount,CloseDate From Opportunity where AccountId =: act.id]; // suppose act is holding account record.
            }    
            return oppList;
        }
        set ;
    }
}

 Today one more confusion arraises to me  when i saw a code  and it's saving also - i used to think in getter setter method get will be followed by varaibale name , But in follwing code get is followed by object name --- please see code below

public class MyCustomController {
public Account acc;
public MyCustomController(){
acc=[select name,id,phone,industry,website,rating,BillingCity,BillingCountry,
ShippingCity,ShippingCountry,AnnualRevenue from Account where id=:Apexpages.currentPage().getParameters().get('id')];
}
public Account getAccount() { // just see get is followed by Object Name i used to think it must be variable name i was accepting it must be only getacc()------- ?????
return acc;
}
public PageReference saveMethod()
{
update acc;
PageReference pf=new apexPages.StandardController(acc).view();
return pf;
}

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Subha ASubha A
Hi Shiv,

Let me explain the difference...

When we user a property in the VF page then it will look for the getter of that property of the Name which starts with get'PropertyName'.

Still confused? Let me explain with the example that you posted:

In first approach:

you might be using a list with name oppList and hence it has accepted the getter with the same name getOppList()

In second approach

The property oppList is used and it is declared as public (which was not done earlier) hence the get method is used which is similar to getOppList which was defined in the previous approach.

In Third Approach

It is again same as first approach but I think you got confuse with the naming convension..

I just want to make a conclusion that in salesforce it will look for the getter of the property in two ways

1. either the property name made public with get; and set; methods
2. public method which matched get'PropertyName'

Hope this will clear your confusion

Thanks
Subha

All Answers

Subha ASubha A
Hi Shiv,

Let me explain the difference...

When we user a property in the VF page then it will look for the getter of that property of the Name which starts with get'PropertyName'.

Still confused? Let me explain with the example that you posted:

In first approach:

you might be using a list with name oppList and hence it has accepted the getter with the same name getOppList()

In second approach

The property oppList is used and it is declared as public (which was not done earlier) hence the get method is used which is similar to getOppList which was defined in the previous approach.

In Third Approach

It is again same as first approach but I think you got confuse with the naming convension..

I just want to make a conclusion that in salesforce it will look for the getter of the property in two ways

1. either the property name made public with get; and set; methods
2. public method which matched get'PropertyName'

Hope this will clear your confusion

Thanks
Subha
This was selected as the best answer
Shiv ShankarShiv Shankar

Subha thanks for explanation, it's really helpful for me.

 

Subha i have seen lot of developers write code with below approach also.

 

public class PrintOpp{
    public List<opportunity> oppList{get;set;}
    
    public List<opportunity> getoppList(){
        if(oppList == null){
            oppList = new List<opportunity>();
            oppList = [select Name,Amount,CloseDate From Opportunity where AccountId =: act.id]; // suppose act is holding account record.
        }    
        return oppList;
    }
}

what i thinks 

 

property should return null because get is not having any body .....

but no,it goes to call getter methods and return results......

 

Subha ASubha A
As I told you earlier it will search for a getter method of the property it can be defined as get;set; or getOppList . If you use both it will go to getOppList only
Shiv ShankarShiv Shankar

Thanks Subha,

 

i just implemented code the way you have explained, my confusion is no more, once again thanks so much.......

Naveen Kuamar GadigeNaveen Kuamar Gadige
public class PrintOpp{ public List<opportunity> oppList{ get{ if(oppList == null){ oppList = new List<opportunity>(); oppList = [select Name,Amount,CloseDate From Opportunity where AccountId =: act.id]; // suppose act is holding account record. } return oppList; } set ; } }

I need test class for above class scenario  i am able to write test class for ,I could  not able to cover:
  public List<opportunity> oppList{ get{ if(oppList == null){ oppList = new List<opportunity>(); oppList = [select Name,Amount,CloseDate From Opportunity where AccountId =: act.id]; // suppose act is holding account record. } return oppList; } set ; }

Please help on this...
Thanks,