LearnFrenzy
Sign in Take a free test

Static and Final Keywords · Predict the Output · Question 19

A local variable hiding a static field Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 19 Back to Exercise

What is the output of the following Java program?

Main.java
public class Main {
    static int total = 100;

    static void add(int total) {
        total += 5;                 // the parameter
        Main.total += total;        // the static field
    }

    public static void main(String[] args) {
        add(10);
        int total = 1;
        System.out.println(total + " " + Main.total);
    }
}
  • A 1 115 Correct
  • B 1 110
  • C 115 115
  • D 1 100
Correct Answer: Option A

Inside add, total is the parameter (10 → 15) and Main.total is the field (100 + 15). In main the local total hides the 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