Private Member Access Within a Class
Consider the following BankAccount class definition.
public class BankAccount {
private double balance;
private String accountNumber;
public BankAccount(String num, double bal) {
accountNumber = num;
balance = bal;
}
public void transfer(double amount, BankAccount other) {
if (amount > 0 && amount <= this.balance) {
this.balance -= amount;
other.balance += amount;
}
}
public double getBalance() {
return balance;
}
}
Which statement best explains why the transfer method can directly access the private instance variable other.balance?
A
Methods can access private fields of any object of the same class type passed as a parameter
B
Private fields become accessible to all methods when objects are passed by reference
C
The transfer method has special privileges because it modifies multiple objects
D
The balance field is automatically made public when passed as a parameter
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 2 | 0m 00s | 90 |
| #2 | y.seong2027 | 0 | 1 | 3m 02s | -192 |
| #3 | bommasam000 | 1 | 9 | 28m 05s | -1,665 |
Items per page:
10
1 – 3 of 3
APFIVE