Skip to content

Commit d161407

Browse files
committed
Add static class info
1 parent 02fbbbb commit d161407

File tree

1 file changed

+62
-14
lines changed

1 file changed

+62
-14
lines changed

docs/ff-concepts/adding-customization/code-file.md

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,40 +176,88 @@ To create an instance of a custom class, open the **Set from Variable** dialog a
176176
</div>
177177
<p></p>
178178

179-
<!-- :::tip[When You Don't Need an Instance]
179+
## Using Custom Class
180+
181+
Once the custom class is added successfully, you can access its fields and methods in the Variable Dialog, call its methods in the Action Flow Editor, assign instances to state variables, pass them to page or component parameters, and use enum values in dropdowns or conditionals.
182+
183+
### Custom Class as Data Type
184+
185+
You can select your custom class as a Type for variables, state, or parameters, just like a [Custom Data Type](../../resources/data-representation/custom-data-types.md).
186+
187+
![custom-class-as-data-type.avif](imgs/custom-class-as-data-type.avif)
188+
189+
### Access Fields and Methods
180190

181-
You can skip instance creation when everything in the class is `static`, meaning it's shared across all uses (like a utility class). For example, look at the class below:
191+
You can use custom class fields to display values directly in the UI, and call its methods in variable dialogs to return a result.
192+
193+
![access-fields-methods.avif](imgs/access-fields-methods.avif)
194+
195+
### Call In Action Flows
196+
197+
You can also add your custom class’s methods directly within an Action Flow. For example, you can trigger the `markHelpful()` method when a user taps a “Mark as Helpful” button to update a field or increment the helpful count of a review.
198+
199+
## Using Static Classes
200+
201+
Sometimes, you just want to run a small piece of logic like formatting text or doing a calculation, without needing to set up anything extra. That’s when `static` data members are helpful. You can access static fields and methods without creating an instance of a class. You can think of them like tools that are always ready to use.
202+
203+
This approach is typically used for **stateless utility classes** where shared functionality is needed across the app. For example, look at the class below:
182204

183205
```jsx
184206
class Utils {
185207
static int square(int x) => x * x;
186208
}
187209
```
188210

189-
In such cases, you can directly access the class data and methods via the **Set from Variable** menu.
211+
The `Utils` class contains a static method `square` that returns the square of a number without needing to create an object of the class.
190212

191-
![static-class-methods.avif](imgs/static-class-methods.avif)
192-
::: -->
213+
Here are couple more examples to understand it better:
193214

194-
## Using Custom Class
215+
- This `StringFormatter` class below provides reusable static methods to capitalize text, convert it to lowercase, or format it in snake_case.
195216

196-
Once the custom class is added successfully, you can access its fields and methods in the Variable Dialog, call its methods in the Action Flow Editor, assign instances to state variables, pass them to page or component parameters, and use enum values in dropdowns or conditionals.
217+
```jsx
218+
class StringFormatter {
219+
static String capitalize(String input) =>
220+
input[0].toUpperCase() + input.substring(1);
197221
198-
### Custom Class as Data Type
222+
static String toLowerCase(String input) => input.toLowerCase();
199223
200-
You can select your custom class as a Type for variables, state, or parameters, just like a [Custom Data Type](../../resources/data-representation/custom-data-types.md).
224+
static String toSnakeCase(String input) =>
225+
input.replaceAll(' ', '_').toLowerCase();
226+
}
227+
```
201228

202-
![custom-class-as-data-type.avif](imgs/custom-class-as-data-type.avif)
229+
- The `MathHelper` class offers handy static methods to calculate tax, apply discounts, find percentages, and round off numbers.
203230

204-
### Access Fields and Methods
231+
```jsx
232+
class MathHelper {
233+
static double calculateTax(double amount) => amount * 0.18;
205234
206-
You can use custom class fields to display values directly in the UI, and call its methods in variable dialogs to return a result.
235+
static double applyDiscount(double amount, double discountPercent) =>
236+
amount - (amount * discountPercent / 100);
207237
208-
![access-fields-methods.avif](imgs/access-fields-methods.avif)
238+
static double calculatePercentage(double part, double total) =>
239+
(part / total) * 100;
240+
241+
static int roundOff(double value) => value.round();
242+
}
243+
```
244+
245+
:::warning
246+
247+
Static classes are powerful, but they should be used carefully. Overusing static methods can lead to less flexible code and potential issues, especially when the logic requires access to state or needs to evolve over time. Stick to static methods only when the logic is truly independent and doesn’t rely on instance-specific data.
248+
249+
:::
250+
251+
### Access Static Fields and Methods
252+
253+
You can access the static class data and methods directly via the ****Set from Variable**** menu.
254+
255+
![static-class-methods.avif](imgs/static-class-methods.avif)
209256

210257
### Call In Action Flows
211258

212-
You can also add your custom class’s methods directly within an Action Flow. For example, you can trigger the `markHelpful()` method when a user taps a “Mark as Helpful” button to update a field or increment the helpful count of a review.
259+
[To come…]
260+
213261

214262
## Custom Enums
215263

0 commit comments

Comments
 (0)