001-003-011

Shadowing Instance and Local Variables

Hard

Problem Description

Shadowing Instance and Local Variables

In this problem, you will create a program that resolves name conflicts (shadowing) between an instance variable and a method parameter using the this keyword, performs counter addition, and displays the result to standard output.

Learning Objective: Understand shadowing when instance and local variables have the same name, and how to use the this keyword

Overview

Verify behavior when instance variable and method parameter have the same name.

Specifications

  • Counter class:
    • Instance variable int count initialized to 0
    • add(int count): adds parameter count to instance variable count
    • getCount(): returns instance variable count
  • In main, create Counter, add 5, add 10, and output final value

Output Format

Adding: 5
Adding: 10
Final count: 15

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up