In Java’s AWT, which is the method used to retrieve the current state of a…

2012

In Java’s AWT, which is the method used to retrieve the current state of a check box?

Answer: A. getState()Concept. In Java’s core libraries a method name is read prefix-first: the verb at the front, not the noun after it, fixes what the method does. The JavaBeans…

  1. A.

    getState()

  2. B.

    putState()

  3. C.

    retrieveState()

  4. D.

    writeState()

Attempted by 37 students.

Show answer & explanation

Correct answer: A

Concept. In Java’s core libraries a method name is read prefix-first: the verb at the front, not the noun after it, fixes what the method does. The JavaBeans convention, which every AWT and Swing component follows, gives each readable property X a no-argument reader getX() — or isX() when the property is a boolean — and, when that property is writable too, a mutator setX(value); a read-only property simply has no setter. The JDK keeps its other prefixes for different jobs: put inserts into a container, write emits to a stream. Naming the method that reads a component’s current property value is therefore an application of the accessor convention, not a choice between plausible English verbs.

Application. A check box in the AWT is the class java.awt.Checkbox. Its ticked/unticked condition is exposed as a single boolean property named state. Applying the convention to that property, step by step:

  1. Identify the property and its type: the property is named state and is a booleantrue when the box is ticked, false when it is clear.

  2. Apply the reader rule to that name: the JavaBeans reader for a property named state is getState(). Oracle’s API declares it on the class as public boolean getState() — no arguments, returning that boolean.

  3. Confirm the pair: on this class state is a read-write property, so alongside the reader it carries the mutator setState(boolean), which sets the box programmatically. Naming the property therefore fixes both members of the pair.

Contrast. Each of the other names offered carries a different prefix, and each prefix does a different job:

  • putState()put is the collections insertion prefix, as in Map.put(key, value). It stores a value the caller hands in; it does not return one the component already holds.

  • retrieveState()retrieve is a domain-level verb from application code. The JDK’s component classes do not use it as an accessor prefix, and no such method is declared on java.awt.Checkbox.

  • writeState()write is the I/O output prefix, as in ObjectOutputStream.writeObject(o). It sends data outward to a stream instead of returning it to the caller.

Cross-check. The same rule predicts every other AWT reader — TextField.getText(), Component.getName(), Choice.getSelectedItem() — which shows the convention was applied rather than guessed. One footnote for modern code: the Swing replacement javax.swing.JCheckBox inherits from AbstractButton and reads its selection with isSelected(), the is form the convention allows for booleans, while the AWT Checkbox asked about here keeps getState().

Result. The current state of an AWT check box is read with getState().

Explore the full course: Mppsc Assistant Professor Computer Science Paper 2

Loading lesson…