16
votes
Accepted
Simple getter setter
I finally hunted down a deep dive into how properties work that you should read top to bottom: Apex Properties from the Apex Developer Guide.
If you want to declare a setter, you must set the ...
13
votes
why we use {get; set;} syntax in wrapper class or in controller class
{get; set;} can appear in any apex class, next to any class variable (thus making it a 'property').
The <access identifier> <type> <variable name> {get; set;} pattern is simply a ...
9
votes
Accepted
LWC best practices: Getters for derived values vs. setters for controlling values
You should prefer method A. Hint: the system calls all getters and then checks against the previous version using === to decide what to redraw. Therefore, B doesn't actually perform any better than A, ...
8
votes
Accepted
Loop in javascript setter takes 1 second per iteration
On the assumption this is this performance issue, this is effectively a bug in the LWC infrastructure. The workaround is to ensure each component accepts the array in an API property with a setter and ...
6
votes
Accepted
How to access Class members decorated using { get; set; }
From Apex, you reference it the same as if it didn't have a getter or setter defined. The fact that it is private means you will need to use the @TestVisible annotation if you want to test against it, ...
5
votes
Public Variable Returns Null
Adding to Eric's answer, the technical name for what you have done here (declaring a variable with the same name in an inner scope) is called "shadowing".
public class MyClass{
// This property ...
5
votes
Accepted
Public Variable Returns Null
You have declared both a local and class variable, remove the local declaration in your method
public class example_Controller {
public boolean booleanFlag {get; set;}
public String ...
5
votes
why we use {get; set;} syntax in wrapper class or in controller class
Take a look at the Visualforce Developer Guide (page 120):
Defining Getter Methods
One of the primary tasks for a Visualforce controller class is to give developers a way of displaying ...
5
votes
Accepted
getter vs setter in LWC
tl;dr Avoid changing data in a getter.
A getter can technically do everything a setter can do. However, performing transformations in the getter are generally a bad idea from a programming perspective ...
5
votes
Accepted
Accessing Private Setters Through Client Code
A private attribute is accessible anywhere in the top-level class.
Consider this class:
public class Messenger {
public class Message {
public String data { get; private set; }
}
public ...
5
votes
Accepted
If I use a { get; set; } on a field in a Global class, will that get/set be public or global?
You can set the access modifier for the property itself using syntax as covered in the documentation:
public class BasicClass {
// Property declaration
access_modifier return_type property_name {...
4
votes
why we use {get; set;} syntax in wrapper class or in controller class
If you are capturing the data from UI or from external interfaces and want to store the those attibute values inside a class, you have to define them as attributes.
getter - returning value of the ...
4
votes
Accepted
Setter not running on List.add()
In order to understand what's happening, you need to mentally separate the variable from the object it contains.
Initially, when you declare a variable, the variable is empty ("null"); this is an ...
4
votes
Accepted
get set method for value in class
This variable is using the default access, private. You won't be able to "see" it outside of the class it is defined in or in a Visualforce page. If you want to be able to use it in Visualforce or ...
4
votes
Accepted
Unable to pass an array from parent LWC to child LWC setter method
An inner mutation like calling push() on an array doesn't trigger the framework to rerender or fire setters. The framework is observing changes to the scopeValues property in the parent, but not to ...
3
votes
Accepted
Visualforce getters are called before setter when selecting a select option
The change occurs because Visualforce has a built-in mechanism that validates that the incoming values are within the list of legal values for the page. More generally speaking, the platform makes no ...
3
votes
How to access Class members decorated using { get; set; }
To clarify the syntax (if hits wasn't static):
count.hits = 1; // calls the "set"
Integer foo = count.hits; // calls the "get"
You can't call count.getHits(). The calling syntax looks just like ...
3
votes
Accepted
Problem with Rendered attribute behaving incorrectly. Seems to always return false when the page is loaded?
You should write:
<apex:outputText rendered="{!userNeedsVerify}" value="firstRender"/>
3
votes
Accepted
Apex Final Property Behaves Like a Member Variable
If you don't provide a property for the final default getter setter property, it also runs just fine instead of "System.FinalException: Final variable has already been initialized".
...
3
votes
Unable to pass an array from parent LWC to child LWC setter method
There is still one use case for @track. When a field contains an object or an array, there's a limit to the depth of changes that are tracked. To tell the framework to observe changes to the ...
2
votes
Accepted
What is the behavior of a class attribute setter defined inline?
With the example class provided, we can run it as anonymous apex along with the following snippet
MySetterTest mst = new MySetterTest();
system.debug('initial values: testMap = ' + mst.testMap + ' ...
2
votes
why we use {get; set;} syntax in wrapper class or in controller class
This is how Visualforce framework works.
Since this a MVC architecture so, framework (View) requires a common medium to interact with Controller. So, for the properties framework expect two methods:-
...
2
votes
How to assign value for getter setter variable from Apex test class
A aInstance = new A();
aInstance.obj = new Account();
I recommend you to take a look into unit-tests trailhead and Apex basics
2
votes
Why is the setter method not working in the following code?
You are getting this error because there is no getter method defined in controller for variable "Oportunities" and it is used in VF page:
<apex:inputText value="{!Oportunities}"/>
To overcome ...
2
votes
Accepted
How to invoke controller setter method in trigger beforeInsert()?
Even if a trigger handler could call a Visualforce page's controller (well, it can, but only if you go through contortions to do so), it shouldn't. A trigger executes in many different contexts and ...
2
votes
Accepted
Need help to cover {get; set;} methods on a TestClass
Covering those lines is easy, and you may want to use an assert to do it. You can do it two ways.
Easy (and not best practice)
DirectDebitController.AccountAmount aa = new DirectDebitController....
2
votes
When to use getter and setter instead of simple property (@api or @track) declaration in LWC?
Generally, GET method returns value and SET method sets the value. We will follow the same way as in other OOPS Programming Language, JAVA.
To understand this exercise, we will see when user will ...
2
votes
When to use getter and setter instead of simple property (@api or @track) declaration in LWC?
getter is used in cases where you want to want to do more operations/modify the data. what you do with @track and @api is an assignment. using a getter you can modify the assigned values before ...
2
votes
Accepted
How can I set; a map<String,String> in one method and use it in another method?
It looks like you're trying to call these methods from an Aura or LWC component. Presumably, these are two separate calls made by the component. Because the entire Lightning framework is a "...
2
votes
@api setter invalid reassignment of public property
An LWC should not directly update its API properties as covered in the documentation:
Using getters and setters ensures that the public API contract is easily enforced. Don’t change the value of a ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
setter × 39getter × 27
apex × 24
visualforce × 11
lightning-web-components × 10
javascript × 4
controller × 4
unit-test × 3
code-coverage × 2
properties × 2
callout × 1
visualforce-component × 1
class × 1
list × 1
exception × 1
before-trigger × 1
customsetting × 1
javascript-controller × 1
code × 1
object × 1
standardcontroller × 1
navigation × 1
null-pointer × 1
set × 1
rendered × 1