Activity Forums Salesforce® Discussions What Is Property in Salesforce Apex?

  • suniti

    Member
    July 10, 2018 at 10:00 am

    Hello Sanjana

    Salesforce 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.

    Thank you.

  • Parul

    Member
    September 14, 2018 at 9:32 am

    Property definitions include one or two code blocks, representing a get accessor and a set accessor:

    • The code in a get accessor executes when the property is read.
    • The code in a set accessor executes when the property is assigned a new value.

    Public class BasicClass {

    // Property declaration
    access_modifier return_type property_name {
    get {
    //Get accessor code block
    }
    set {
    //Set accessor code block
    }
    }
    }

     

    Thanks

  • Anjali

    Member
    September 14, 2018 at 9:36 am

    Hi Sanjana

    An Apex property is similar to a variable; however, you can do additional things in your code to a property value before it is accessed or returned. Properties can be used to validate data before a change is made, to prompt an action when data is changed (such as altering the value of other member variables), or to expose data that is retrieved from some other source (such as another class).

    Property definitions include one or two code blocks, representing a get accessor and a set accessor:

    • The code in a get accessor executes when the property is read.
    • The code in a set accessor executes when the property is assigned a new value.

    If a property has only a get accessor, it is considered read only. If a property has only a set accessor, it is considered write only. A property with both accessors is considered read-write.

    Hope this may help you.

    Thankyou!

  • shariq

    Member
    September 14, 2018 at 11:59 am

    Hi,

    An Apex property is similar to a variable.

    Property definitions include one or two code blocks, representing a get accessor and a set accessor:

    The code in a get accessor executes when the property is read.
    The code in a set accessor executes when the property is assigned a new value.

    Note the following:

    The body of the get accessor is similar to that of a method. It must return a value of the property type. Executing the get accessor is the same as reading the value of the variable.
    The get accessor must end in a return statement.
    We recommend that your get accessor not change the state of the object that it is defined on.
    The set accessor is similar to a method whose return type is void.
    When you assign a value to the property, the set accessor is invoked with an argument that provides the new value.
    When the set accessor is invoked, the system passes an implicit argument to the setter called value of the same data type as the property.
    Properties cannot be defined on interface.

    Hope this helps!

  • Avnish Yadav

    Member
    September 14, 2018 at 1:14 pm

    Hello,

    When u used property u can control get, set, access.

    In this example, if accountList = null and you try to get this value, at first accountList queried and return you list of account.
    `
    public Account[] accountList {
    get {
    if (accountList == null) {
    accountList = [SELECT Id, Name FROM Account];
    }

    return accountList;
    }
    }
    `

    Tracking value changes. In this example when you set a or b value, sum property recalculate automaticaly.
    `
    public Integer sum {
    get;
    set;
    }

    public Integer a {
    get {
    if (a == null) a = 0;
    return 0;
    }

    set {
    sum = a + b;
    }
    }

    public Integer b {
    get {
    if (b == null) b = 0;
    }

    set {
    sum = a + b;
    }
    }
    `
    and more other examples.

    Thanks.

Log In to reply.

Popular Salesforce Blogs