LearnFrenzy
Sign in Take a free test

Classes and Objects · Find the Error · Question 3

Error: reading a private field from another class Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
class Account {
    private double balance = 500;
}

public class Main {
    public static void main(String[] args) {
        Account a = new Account();
        System.out.println(a.balance);
    }
}
  • A balance must be int
  • B balance is private to Account, so Main cannot access it; add a public getter such as getBalance() Correct
  • C Account must be public
  • D println cannot print doubles
Correct Answer: Option B

Hiding fields is the basis of encapsulation.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links