Anatomy of a Class

AP Computer Science A· difficulty 2/5

public class Box {
    private final int CAPACITY;
    public Box(int c) { CAPACITY = c; }
    public int getCap() { return CAPACITY; }
}
// ...
Box b = new Box(50);
System.out.println(b.getCap());

What is printed?

  • A

    50

    check_circle
  • B

    Compile-time error: cannot assign final

  • C

    0

  • D

    null

Explanation

A blank final instance variable can be assigned exactly once in the constructor; this is allowed.

Want 10 more like this — adaptive to your weak spots?

Related questions