Activity Forums Salesforce® Discussions What Is Property In Salesforce Apex? Explain With Advantages?

  • shariq

    Member
    September 20, 2018 at 3:09 pm

    Hi,

    Apex mainly consists of the syntax from the well known programming language Java. As a practice of encapsulation in java we declare any variable as private and then create the setters and getters for that variable.

    private String name;

    public void setName(String n)

    {

    name = n;

    }

    public String getName()

    {

    return name;

    }

    However, the Apex introduced the new concept of property from language C# as shown below:

    public String name {get; set;}

    As we can see how simple the code is and instead of using nearly 8 to 11 lines all done in 1 line only. It will be very useful when lots of member is declared in Apex class. It has another advantage in “number of lines of code” limit by salesforce which will drastically reduced.

    Thanks

  • Parul

    Member
    September 20, 2018 at 6:14 pm

    Here is some points more:

    Apex property is similar to a variable, they can validate data before a change is made; they can prompt an action when data is changed, such as altering the value of other member variables; or they can expose data that is retrieved from some other source, such as another class.

    Thanks

  • shariq

    Member
    September 20, 2018 at 10:52 pm

    Hi,

    Example -

    <apex:page controller="simplegetset">
    <apex:form>
    <apex:outputlabel value="Enter your name here"/>
    <apex:inputtext value="{!userinput}"/>
    </apex:form>
    </apex:page>

    The Apex code for this page would be...

    public class simplegetset
    {
    public String userinput{get; set;}
    }

  • Parul

    Member
    September 21, 2018 at 3:45 am

    Hi,

    Apex introduced the new concept of property from language shown below:
    publicString name {get; set;}

    A setter method that assigns the value of this attribute to a class variable in the associated custom component controller. If this attribute is used, getter and setter methods, or a property with get and set values, must be defined.

  • Anupam

    Member
    July 13, 2022 at 2:38 pm

    Please stop pasting copied answers from Salesforce docs. People couldn't understand the answer there that's why they come to these forums. If they sees the same answer here then what's the point of having these forums?

  • GetOnCRM

    Member
    July 15, 2022 at 7:02 am

    Apex mainly consists of syntax from the well-known programming language Java. As a practice of encapsulation in Java, we declare any variable as private and then create the setters and getters for that variable.
    Apex mainly consists of syntax from the well-known programming language Java. As a practice of encapsulation in Java, we declare any variable as private and then create the setters and getters for that variable.
    private String name;
    public void setName(String n)
    {
    name = n;
    }
    public String getName()
    {
    return name;
    }
    However, the Apex introduced the new concept of property from language C# as shown below:
    public String name {get; set;}
    We can see how simple the code is, and instead of using nearly 8 to 11 lines, all done in 1 line only. It will be very useful when many members are declared in Apex class. It has another advantage in salesforce's "number of lines of code" limit, which is drastically reduced.

  • CRMJetty

    Member
    August 9, 2022 at 4:17 am

    Hello Aman,Refer to this salesforce Document: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_properties.htmHope this helps you.Thanks.

  • vishakha

    Member
    October 6, 2022 at 12:23 pm

    In Apex, variables are declared as private by default following the capsulation practice from Java. To make these variables accessible outside the class, setters and getters need to be created. Read about it here: https://www.salesforcetutorial.com/what-is-property-in-apex-salesforce/

Log In to reply.

Popular Salesforce Blogs