levo

Objects, Classes and Data Structures

Getters, setters and validating an object's state

  • 3 things to know
  • AS Unit 1

Overview

Accessor method, mutator method and java naming convention for these two kinds of method.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is a mutator method?

Key term

Why is a getter preferred to simply making the field public?

Key term

State the Java naming convention for getter and setter methods

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is an accessor method?

    Model answer

    An accessor method, usually called a getter, is a public method whose only job is to return the current value of a private attribute.

  2. 2

    What is a mutator method?

    Model answer

    A mutator method, usually called a setter, receives a new value for a private attribute and stores it, normally only after checking that the value is acceptable.

  3. 3

    State the Java naming convention for these two kinds of method.

    Model answer

    Java convention names a getter getAttribute and a setter setAttribute, and a getter for a boolean attribute is normally named isAttribute.

  4. 4

    Why is a getter preferred to simply making the field public?

    Model answer

    A getter keeps the attribute private, so the class is free to validate, record, calculate or restructure the value behind the method, none of which is possible once outside code reads the field directly.

  5. 5

    A setter is written as public void setAge(int a) { if (a >= 0 && a <= 120) { age = a; } }. What happens when setAge(-3) is called?

    Model answer

    The test fails because -3 is below zero, so the assignment never runs and the attribute age keeps whatever valid value it already held.

  6. 6

    Criticise a setter that stores whatever value it is handed.

    Model answer

    A setter with no check adds a method call but no protection, so the attribute is effectively public and an invalid value still reaches the object.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Software and the Development Environment
Defining Data: Types, Variables and Classes
Program Control Structures
Objects, Classes and Data Structures

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A null is the of one.
absencepresencetype

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AA null means no value has been recorded.
BZero and an empty string are values.
CA null is the absence of a value.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Objects, Classes and Data Structures, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.