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
KaityKaity 

private set usage in apex controller

Hi,
What is the difference between set and private set? And when do we use private set?
example: 
public ApexPages.StandardSetController ssc {get; private set;}

Please advice.

-Kaity
PratikPratik (Salesforce Developers) 
Hi Kaity,

You can refer to this document:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_properties.htm

In your code as mentioned above, it's public to read value but it's private to write value.

Thanks,
Pratik

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
KaityKaity
Thanks Pratik for responding. I read this document. However, can you please elaborate this.

-Kaity
PratikPratik (Salesforce Developers) 
Hi Kaity,

Get and Set are properties to read and write the values respectively. To set their accessibility within or outside of the class, we use private, public, protected access mofifiers.

Please go through the example.

Example:

global virtual class PropertyVisibility {
   // X is private for read and public for write
   public integer X { private get; set; }
   // Y can be globally read but only written within a class
   global integer Y { get; public set; }
   // Z can be read within the class but only subclasses can set it
   public integer Z { get; protected set; }
}

Thanks,
Pratik
KGDevKGDev
this was a great explanation! not sure why the author of the question did not mark it as Answer.