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 balance field is automatically made public when passed as a parameter
D
The transfer method has special privileges because it modifies multiple objects
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE