012-001-008

static Modifier: Shared Class Variables

Medium

Problem Description

static Modifier: Shared Class Variables

Learning Objective: Understand how to use static fields to share data across all instances

Overview

Create a Counter class and use a static variable to count how many instances have been created. Verify that all instances share the same counter.

Specifications

  • Counter class static field: count (int type)
  • Constructor: Increments count when instance is created
  • Static method getCount(): Returns current count value
  • In main method, create 3 instances and output count value

Output Format

Total instances: 3

Ready to Try Running Code?

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

Don't have an account?