levo

Objects, Classes and Data Structures

Getters, setters and validating an object's state

  • 3 things to know
  • AS Unit 1
Start revising · £5/month

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.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

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

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A null is the of one.
absencepresencetype

Blank Blitz consolidates the facts

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

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

An error is the mistake a person made.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

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