C is correct I`v tested it. The order of execution: {!StringMethod} invokes getStringMethod() that calls myString getter in which we have assignment myString = a, and than getter returns first 'a'. Both next {!myString} invokes myString variable getter, so we have 'a' and 'a' again. And getMyString() metod is never called.
The question seems to be wrong since there is no getStringMethod defined - but assuming that it's what "StringMethod" was supposed to reference - the answer is a, a, a
Even though getStringMethod is called before the getter, the method then attempts to retrieve the value to check if it's null. This retrieval calls the getter - and causes the value to be set to 'a' before being returned.
Basically, the property is null only as long as you don't check what it is - the first attempt to check its value changes its state.
I tested the full code in Dev console and the answer is: a, a, a
I've struggled to understand the reason, but it seems that getStringMethod uses the getter to access myString, so it receives the value 'a' and doesn't enter the If statement at all
Just test this code in dev console :
String myString {
get {
if (myString == null) {
myString = 'a';
}
return myString;
} private set;
}
system.debug(myString == null);
You get a 'false' printed. Because as soon as you try to evaluate if myString == null in the debug, you enter in the getter of myString, who evaluate myString to null, and set it to 'a'. So myString is no longer null and the system.debug(myString == null) = false :)
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
irina_735
1 year, 1 month agogovosen887
1 year, 7 months agoGusfn
2 years, 1 month agolmeloni91
2 years, 5 months agonoox
2 years, 10 months agoOMsairam
2 years, 11 months agonoox
2 years, 11 months ago