In which two ways can the developer declare the variables to ensure their value can only be assigned one time?

A developer is implementing an Apex class for a financial system, Within the class, the variables ‘creditAmount’ and "debitAmount’ should not be able to change once a value Is assigned.

In which two ways can the developer declare the variables to ensure their value can only be assigned one time? Choose 2 answers
A . Use the static keyword and assign its value in the class constructor,
B . Use the final keyword and assign its value in the class constructor.
C . Use the static keyword and assign its value in a static initializer.
D . Use the final keyword and assign its value when declaring the variable.

Answer: B, D

Explanation:

To ensure that the variables creditAmount and debitAmount cannot change once assigned, the final keyword should be used.

Option B: Use the final keyword and assign its value in the class constructor.

Declaring a variable as final allows it to be assigned once.

Assigning the value in the constructor is acceptable for instance variables.

Option D: Use the final keyword and assign its value when declaring the variable.

Assigning the value at declaration ensures it cannot be changed later.

Why Not Other Options:

Option A: Using static with assignment in the constructor is invalid because static variables belong to the class, not instances.

Option C: Using static and assigning in a static initializer doesn’t prevent reassignment unless final is also used.

Reference: Final Variables:

"You can declare instance variables as final. A final variable can only be assigned a value once, either when you declare it or in the constructor."

― Apex Developer Guide: Classes, Objects, and Interfaces

Latest DEX-450 Dumps Valid Version with 456 Q&As

Latest And Valid Q&A | Instant Download | Once Fail, Full Refund

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments