Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
public class Main {
public static void main(String[] args) {
Account acc = new Account();
acc.deposit(100);
System.out.println(acc.balance);
}
}
class Account {
private int balance;
void deposit(int amount) { balance += amount; }
}It does not compile
- Expecting
100:privateclosesbalanceto every other class, andMainis another class even though it sits in the same file. - Expecting a run-time error: access rules are checked by the compiler, which reports
balance has private access in Account.
- How would you let
mainfind out the balance without opening the field? - Which classes can see a field declared without any access modifier?