012-001-009
Count Instances with Static Variable
Hard
Problem Description
Count Instances with Static Variable
In this problem, you will implement a Counter class with private static int count = 0 that increments count in its constructor. In the main class, read an integer n from standard input, create n instances, retrieve the total count via Counter.getCount(), and print it to standard output.
Input: An integer n (number of instances to create)
Output: Instances created: n
Learning Objectives:
- Understand how static variables are shared across all instances of a class
- Apply the pattern of updating a static variable inside a constructor
- Access a class-level variable through a static method without creating an instance
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