Skip to content
CodeKruPro

Count how many objects of a class were created

QuizEasy-MediumJava onlyAsked in: Infosys

You want Counter.getCount() to return how many Counter objects have been created. Which design works?

  • A private static int count field, incremented in the constructor, returned by a static getter
  • A private int count field, incremented in the constructor
  • A static counter incremented in a static block
  • A local variable in the constructor

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in