QUESTION IMAGE
Question
which statement about the following class is correct? 1 class bankaccount 2 declarations 3 private num currentbalance = 0 4 public num getcurrentbalance() 5 return this.currentbalance 6 public void depositfunds(num amount) 7 this.currentbalance = this.currentbalance + amount 8 return 9 endclass a. the use of this. in front of data fields in lines 5 and 7 indicates they have private access. b. the use of this. in front of data fields in lines 5 and 7 will cause an error. c. the use of this. in front of data fields in lines 5 and 7 is required. d. the use of this. in front of data fields in lines 5 and 7 is optional.
In object - oriented programming, the this keyword is used to refer to the current object. When accessing private data fields within instance methods of a class, using this is optional but can make the code more clear, especially when local variables with the same name as data fields exist. In the given BankAccount class, the use of this in lines 5 and 7 to access the currentBalance private data field is optional as there are no local variables with the same name shadowing it.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
D. The use of this. in front of data fields in lines 5 and 7 is optional.