Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I've encountered an odd bug where Apex code was assigning a value to a static property without a setter defined.

Below is a simplified version of the code:

@isTest
public class PropertySettingTest {

    public static boolean testBooleanProp {
        get {
            return false;
        }
    }

    static testMethod void setPropertyUnitTest() {
        System.assert(!testBooleanProp);

        testBooleanProp = true;

        System.assert(!testBooleanProp);
        System.debug(testBooleanProp);
    }
}

When assigning the property I'd expect to get an error when saving/compiling like:

Save error: Variable is not visible: testBooleanProp

However, is saves and runs. The property assignment has no affect.

I did another test, and if the property isn't static the code fails to save/compile as expected.

Am I missing something or is this a bug? I'd like some confirmation before raising a support case.

share|improve this question
2  
Thanks for pointing this out. This is a defect, and we will address it in the Spring '13 release. – Josh Kaplan Sep 26 '12 at 1:24

1 Answer

up vote 3 down vote accepted

According to the Apex code reference on properties a property with only a get accessor is considered read-only. The reference has a section on static properties and it does not state that they behave any differently.

I performed a quick test and added a set and removed the get and I got the error when trying to read the variable. This inconsistency adds to the case that it is an issue.

public static boolean testBooleanProp {
    set;
}

static testMethod void setPropertyUnitTest() {
    // Causes variable is not visible error due to no get accessor defined.
    System.debug(testBooleanProp);
}

I'm with you that this looks like a defect.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.