0

I've this simple getter Setter property inside class A . public integer rs { get{return rs ;} set{ rs = 7;} }

In anonymous window, when I print using system.debug in this way:

A a1 = new A(); System.debug('Value of Rs' + a1.rs);

It prints null . why ?? why it doesn't recognize values as 7 ?

2 Answers 2

0

The value of rs will be null until something tries to set the value. Setters are only called upon assignment:

   A val = new A();
   a.rs = 42; // The answer to life, the universe, and everything...
   System.assertEquals(7, val.rs, 'Interesting... now we return 7?');

If you want to always return 7, do so in the getter.

0

You are not actually setting the value of rs to anything. You would need to call the setter method in order for it to set the value of rs. In your current example you could write code that would say

a1.rs = 10;

And it would then call your setter method and set the true value of rs=7. However if you just want to return a constant get{ return 7;} and you would not need the setter method.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .