levo

Defining Data: Types, Variables and Classes

Static variables in a class

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

Overview

Static variable, static constant and static method.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is a static variable?

Key term

How does a static variable differ from an instance variable?

Key term

Why is a static variable normally accessed through the class name rather than through an object reference?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a static variable?

    Model answer

    A static variable is a variable declared as belonging to the class itself rather than to any object, so a single copy is shared by every object of that class.

  2. 2

    How many copies of a static variable exist, however many objects are created?

    Model answer

    Exactly one copy of a static variable exists, held with the class, no matter how many objects are created or whether any object exists at all.

  3. 3

    How does a static variable differ from an instance variable?

    Model answer

    A static variable is shared by the whole class and holds one value for all objects, whereas each object receives its own instance variable and can hold a different value in it.

  4. 4

    What are two realistic uses for a static variable?

    Model answer

    A static variable can count how many objects of a class have been created, and it can hold the next account number to be issued so that every object draws from one running source.

  5. 5

    class Account { static int count = 0; Account() { count++; } } — what is the value of Account.count after three objects have been created?

    Model answer

    The value of Account.count is 3, because the single shared counter starts at 0 and the constructor adds one to that same counter each of the three times an object is made.

  6. 6

    Why is a static variable normally accessed through the class name rather than through an object reference?

    Model answer

    A static variable belongs to the class, so using the class name makes clear that the value is shared and that no object is needed to reach it.

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 Defining Data: Types, Variables and Classes, 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.