LearnFrenzy
Sign in Take a free test

Classes and Objects · Predict the Output · Question 1

Two independent objects of one class Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following Java program?

Main.java
class Counter {
    int value;
    void inc() { value++; }
}

public class Main {
    public static void main(String[] args) {
        Counter a = new Counter();
        Counter b = new Counter();
        a.inc(); a.inc(); b.inc();
        System.out.println(a.value + " " + b.value);
    }
}
  • A 2 1 Correct
  • B 3 3
  • C 1 1
  • D 2 2
Correct Answer: Option A

Each object has its own value field.

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