← Unit 3 · all exercises
Saved in this browser only

How many have been made

Write a Robot class that counts how many robots exist. The static method getCount returns that total. Call it as Robot.getCount(), without an object.

class Robot { Robot(); static int getCount(); }

Robot.java
Loading editor…
Results

Examples

  • int before = Robot.getCount(); Robot a = new Robot(); Robot.getCount() - before → 1
  • int before = Robot.getCount(); Robot a = new Robot(); Robot b = new Robot(); Robot.getCount() - before → 2

There are also 2 hidden tests you can't see. Solve the problem, not the examples.