007-002-014

Object Creation: Reference Assignment

Medium

Problem Description

Object Creation: Reference Assignment

In this problem, you will create a Counter class with a count field and an increment() method. The system will test your class to verify object reference assignment and sharing behavior.

Learning Objective: Understand object reference assignment and sharing

Overview

Create a Counter class. The system will verify that multiple variables can reference the same object and that reference sharing works correctly.

Specifications

  • Create a Counter class
  • int count field (initial value 0)
  • void increment() method that increases count by 1

Test Examples

The system will test your class with code like:

Counter c1 = new Counter();
Counter c2 = c1;
c1.increment();
System.out.println(c1.count);  // 1
System.out.println(c2.count);  // 1 (same object)

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