← Unit 3 · all exercises
Saved in this browser only

When the names collide

Write a Box class with a field called size. Its constructor and its setSize method both take a parameter also called size. Use this.size to tell the field apart from the parameter, or the assignment does nothing at all.

class Box { Box(int size); void setSize(int size); int getSize(); }

Box.java
Loading editor…
Results

Examples

  • new Box(5).getSize() → 5
  • Box b = new Box(5); b.setSize(9); b.getSize() → 9

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