• Naveen Kuamar Gadige
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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;
}

}