Skip to main content
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
Derek F's user avatar
  • 62.7k
9 votes
Accepted

Visualforce Private vs Public Getter/Setter Syntax/Structure & Order of Execution

What is happening is that you are actually using an Initialization Block. See Static and Instance Methods, Variables, and Initialization Code (Using Initialization Code). Using Initialization Code ...
Adrian Larson's user avatar
  • 151k
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, ...
sfdcfox's user avatar
  • 497k
8 votes
Accepted

How to test a class that contains no methods

You can still test it, because classes that have no defined constructors still have a default constructor. So, your unit test would look like this: @isTest static void test() { testController c = ...
sfdcfox's user avatar
  • 497k
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, ...
Adrian Larson's user avatar
  • 151k
5 votes
Accepted

Init field with setter and getter

You can set them in single line as well. You just need to follow the format. Which is public class BasicProperty { public integer prop { get { return prop; } set { prop = value; } } ...
Tushar Sharma's user avatar
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
Derek F's user avatar
  • 62.7k
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 ...
Eric's user avatar
  • 54.2k
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 ...
sfdcfox's user avatar
  • 497k
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 ...
sfdcfox's user avatar
  • 497k
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 {...
Phil W's user avatar
  • 37k
4 votes
Accepted

Pull multiple fields from an object in get;set;

You have to use one of three following methods: Include the Field in Your Page If you put the following code in your page, it'll pull the field: <apex:outputText rendered="false" value="{!...
sfdcfox's user avatar
  • 497k
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 ...
Santanu Boral's user avatar
4 votes
Accepted

Need help understanding get/set methods, weird behavior

I have observed Apex properties - a convenient way to implement getters and setters - only being referenced once even though the Visualforce includes multiple references. It seems like there is some ...
Avijit Das's user avatar
  • 1,416
4 votes
Accepted

Overriding Getters and Setters from Base Class

You can do this, but it's not straight-forward. You need to set up the parent and child as follows: global virtual class TestParent { protected virtual String getName() { return ...
sfdcfox's user avatar
  • 497k
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 ...
sfdcfox's user avatar
  • 497k
4 votes

SOQL statement inside a getter/setter - what benefits?

In this example there is no difference than having a getMap() method that always executes the same code as in the getter. Not to be too nitpicky with terms, but what you have is a property and not a ...
nbrown's user avatar
  • 4,033
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 ...
sfdcfox's user avatar
  • 497k
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"/>
Liron C's user avatar
  • 1,788
3 votes

Pull multiple fields from an object in get;set;

You need to call addFields on your StandardController instance. Note that this call isn't supported for controllers you instantiate in the test context. public MatchExtension(ApexPages....
Adrian Larson's user avatar
  • 151k
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 ...
Mike Lockett's user avatar
3 votes
Accepted

Fetching record data from Apex in LWC

You're calling an apex method that has no input parameters, but you're passing two params (recordId and fields). Since you already defined the fields in the apex method, you only need the first ...
RubenDG's user avatar
  • 8,762
2 votes

Need help understanding get/set methods, weird behavior

In salesforce "get" method is used to pass data from your Apex code to your Visualforce page and "set" is used to pass data/value from visualforce page to the controller variable. So basically when ...
Nilesh Borse's user avatar
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
Oleksandr Berehovskyi's user avatar
2 votes
Accepted

How to get related object fields without the id in a query

Find my comments in the code. Step 1: Retrieve Account Information from this SOQL Map<Id, Opportunity> oppsMap = new Map<Id, Opportunity>( [SELECT Id, Account.OwnerId, Account....
Santanu Boral's user avatar
2 votes
Accepted

Need clarification on a getter

I do not think the getter/setter is actually doing anything for you. This is the methods definition: sforce.console.openPrimaryTab(id:String, url:URL, active:Boolean, (optional)tabLabel:String, (...
Halfwarr's user avatar
  • 619
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:- ...
Ashwani's user avatar
  • 22.6k
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 ...
Kritik's user avatar
  • 266

Only top scored, non community-wiki answers of a minimum length are eligible