+ Start a Discussion
sfdcfoxsfdcfox 

Apex Code glitches (?) I just found.

I believe I just found two bugs (?) in the platform that I can't seem to find documentation on. The first is a compilation issue. You can't apparently assign to a private static variable if it has a setter but no getter.

 

public class X {
    private static boolean Y {
        set {
            y = value;
        }
    }

    static {
        x.y = true;  // This line fails to compile: 'Variable is not visible'
    }
}

 The second problem is with code coverage. If a getter/setter is on the same line as the variable declaration, you get code coverage, otherwise, you don't. Example follows.

 

// EXPECTED RESULT: 100% CODE COVERAGE
// ACTUAL RESULT: 66% CODE COVERAGE
public class X { public static boolean Y { get { return true; } } public static boolean Z { // THIS LINE DOES NOT GET CODE COVERAGE get { return false; } }
public static testMethod void test() {
System.assertEquals(true,x.y);
System.assertEquals(false,x.z);
} }

Has this been a problem for anyone else?

 

craigmhcraigmh

I think the first one is because the write-only property Y is private. You shouldn't be able to access Y unless you're inside of X, even though it's static. I would have been surprised if that compiled.

craigmhcraigmh

Yeah, try this:

 

public class X {
    private static boolean Y {
        set {
            y = value;
        }
    }
}

 

Same error, different line. The "y = value;" assignment.

craigmhcraigmh

The second one is definitely a bug...

sfdcfoxsfdcfox

The static block is inside class X, and so should have access to Y. Adding a getter method (even a default getter method) makes the code work correctly. It seems that a getter is required for a class to access its own private static members, which seems buggy to me.