| preferred AP College board partner for AP classes
medium Solved by 2 students
Private Member Access Within a Class
< Prev
Next >

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

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer

Question Leaderboard

Not enough data yet to show leaderboard.

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy