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
mikunmikun 

Get and Set

Hi All,
   I got a doubt regarding this get and set methods . Please check this class.

public with sharing class class2 {
    public string prop1 {
      get { System.debug('ppp get p1'); return prop1; }
      set { System.debug('ppp set p1'); prop1 = value; }
       }
       
       public string prop2;
       public string getprop2(){
        System.debug('ppp get p2');
        return prop2;           
       }
       public void setprop2(string i){
           System.debug('ppp set p2');
           prop2 =i;
       }
 
    public static testMethod void test1(){
        class2 c = new class2();
        System.debug('prop1--'+c.prop1);
        c.prop1 = 'test prop1';
        System.debug('prop2--'+c.prop2);
        c.prop2 = 'test prop2';
    }
}

so when i run this class the get and set of 'prop1' executes but not of 'prop2'. And BTW what is the difference between this two way of writing get and set for a variable.So plz help me understand this.


Thank You,
Mikun

 

Sridhar BonagiriSridhar Bonagiri

Hi,

 

I checked the code it is correct only. where you are checking and saying the prop2 is not working.

 

 

Regards,

Sridhar Bonagiri

mikunmikun

Hi Sridhar ,

   there was an edit i did.'System.debug('prop2--'+c.prop1);' i made it System.debug('prop2--'+c.prop2);

 

Bu yest my doubt is if u run this class in debug log - U can see the message -  'ppp get p1' and 'ppp set p1'  for prop1 get and set but for  prop2 the debug inside get and set not working. plz check...