function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nitish KulkarniNitish Kulkarni 

get set property problem

public class SampleController
{
public string myString{get{
if(myString == null)
{
return 'a';
}
return myString;
}Private set;}
public String getmyString()
{
return 'getmyString';
}
public string getMyStringMethod()
{
if(myString == null)
{
myString = 'b';
}
return myString;
}
}
Visualforce Code
<apex:page controller="SampleController">
{!MyStringMethod}, {!myString}, {!myString}
</apex:page>
what is the output and please explain it . ? 
 
Raj VakatiRaj Vakati
The Output is  a, a, a .. 

Before Going in Details let me explain how get and set will work 
  • 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.

In your code, you gave all GET so your property is read first .. 

When you call the MyStringMethod method it will check the mtStringb value is null or not .But In you Get and Set you are checking the Not null or not and if its nul you are setting to 'A' so its showing a insted of b 


When you call the myString you are not setting any value so its null so you will get the "a" to UI 

 
Nitish KulkarniNitish Kulkarni
You mean Get property will run first and set Mystring = 'a' then MyStringMethod() will return mystring which is also 'a' but when myString() method runs why it is not returning  'getmyString' as mentioned in its method body.?