Class Members: State, Actions, and Access

When you read an object-oriented program, you need to identify what stores information, what performs an action, and which object an expression uses. Class Members brings these decisions together. Here you will use Java to distinguish fields from methods, separate object-specific state from shared state, and follow member access without confusing a method parameter with an object's stored value.

Fields store state

A field is a variable declared directly in a class body. It stores part of the program's state. A variable declared inside a method is a local variable, rather than a field; a parameter receives a value when the method is called.


An instance field belongs to an individual object. Each newly created object receives its own instance fields. A field declared with static belongs to the class and is shared by its objects.


Java
class Counter {
    int value = 0;
    static int calls = 0;

    void add(int amount) {
        value = value + amount;
        calls = calls + 1;
    }

    static int callCount() {
        return calls;
    }
}


Here, value records one counter's current value. The shared field calls records how many times add has run across the counters. It increases by one per call, regardless of the amount added.


The update performed on the selected object's value is:


new value=old value+amount\text{new value} = \text{old value} + \text{amount}


For example, suppose these statements run inside a method:


Java
Counter first = new Counter();
Counter second = new Counter();
first.add(4);
second.add(5);
second.add(2);


The first object's value becomes 4, and the second object's value becomes 7. There have been three calls, so Counter.calls is 3.


A Counter class box connects to two object boxes with separate value fields.



Methods perform actions

A method is a named operation declared in a class. Calling a method executes its body. The parentheses in first.add(4) indicate a call; the value inside them supplies the method's argument. The keyword void means that add returns no value to its caller.


An instance method runs with a current object. In first.add(4), that object is first, so the unqualified name value accesses first's field. Calling the same method through second selects second's field instead.


A static method has no current object. Call it through the class name, as in Counter.callCount(). It can directly access the shared field calls. It can also access an instance field if it has an explicit object to use, such as a parameter named counter followed by counter.value.


Member

Belongs to

Typical access

Instance field

One object

first.value

Static field

The class

Counter.calls

Instance method

Called with a current object

first.add(4)

Static method

The class; no current object

Counter.callCount()


Use class-qualified access for static members. It makes their shared ownership visible when you read the code.

Resolve names before tracing changes

Java resolves a name using its context. Within an instance method, an unqualified name can refer to a field, but a parameter or local variable with the same name takes precedence. This situation is called shadowing.


Java
void setValue(int value) {
    this.value = value;
}


Here, this means the current object. The expression this.value selects its field, while the unqualified value names the parameter. The assignment copies the argument into the object's field.


Resolve each name before calculating a result. Writing value = value in this method would assign the parameter to itself and leave the field unchanged.



What to remember

  • Fields store values; method calls execute operations.
  • Instance fields are separate for separate objects; static fields are shared.
  • Use an object for instance access and the class name for static access.
  • A shared value changes only through executed updates.
  • Use this.fieldName to distinguish an object's field from a same-named parameter.

Example: Tracking Print Submissions

A school print service represents each user's print queue with a Java object. An instance field stores a separate value for each queue, while a static field stores one shared value for the class. The program counts pages submitted during a fresh run; it does not remove or print any pages during this example.


Java
class PrintQueue {
    int pending = 0;
    static int submitted = 0;

    void submit(int pages) {
        pending = pending + pages;
        submitted = submitted + pages;
    }

    int preview(int pending) {
        return this.pending + pending;
    }

    static int totalSubmitted() {
        return submitted;
    }
}


These statements execute inside main:


Java
PrintQueue a = new PrintQueue();
PrintQueue b = new PrintQueue();
a.submit(8);
b.submit(5);


a) Find a.pending, b.pending, and PrintQueue.submitted after these statements.


First, both queues begin with 0 pending pages, and the shared submitted count begins at 0 pages. The call a.submit(8) selects a as the current object. Immediately afterward, a.pending is ________ and the shared count is ________. Queue b still has 0 pending pages.


Next, b.submit(5) selects b, so its pending count becomes 0+5=50 + 5 = 5 pages. This call also increases the shared count:


submitted=8+5=13 pages\text{submitted} = 8 + 5 = 13\ \text{pages}


The answers are a.pending = 8 pages, b.pending = 5 pages, and PrintQueue.submitted = 13 pages. The instance fields remain separate, while both calls update the same static field.


b) The program now executes a.submit(3). Find a.pending and the value returned by PrintQueue.totalSubmitted().


Before this call, a has 8 pending pages and the shared count is ________. The call adds ________ to both the selected object's pending count and the shared submitted count.


a.pending=8+3=11 pages,submitted=13+3=16 pages.\begin{aligned} a.\text{pending} &= 8 + 3 = 11\ \text{pages},\\ \text{submitted} &= 13 + 3 = 16\ \text{pages}. \end{aligned}


The static method totalSubmitted() returns the shared field directly. Therefore, a.pending is 11 pages and PrintQueue.totalSubmitted() returns 16 pages. Queue b remains at 5 pending pages because this call selects a.


c) The program next evaluates a.preview(4). Find its returned value and state whether either field changes.


The argument gives the parameter named pending a value of ________. Inside this instance method, this identifies a, so this.pending selects the object's stored value of ________. The unqualified name pending selects the parameter.


returned value=11+4=15 pages\text{returned value} = 11 + 4 = 15\ \text{pages}


The method contains a return expression but no field assignment. Calculating a preview does not update the stored counts. The answer is 15 pages, with a.pending unchanged at 11 pages and PrintQueue.submitted unchanged at 16 pages.

A Java program uses the following class. Inside another method, Sensor sensor = new Sensor(); has already executed. Which expression calls the instance method and returns the stored sample count?


Java
class Sensor {
    int samples = 6;

    int sampleCount() {
        return samples;
    }
}
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A conference app gives every printed badge a sequential number. Read the Java class below, then complete each sentence about it.


Java
public class Badge {
    private static int nextNumber = 1;
    private int number;

    public Badge() {
        number = nextNumber;
        nextNumber++;
    }

    public int getNumber() {
        return this.number;
    }

    public static void resetNumbering() {
        nextNumber = 1;
    }
}


The keyword in the declaration of nextNumber that makes the class keep a single shared copy is .


Because number is declared without that keyword, every Badge object gets its own copy, so number is an variable.


Inside getNumber, the keyword that refers to the particular object the method was called on is .


If three Badge objects are created and then resetNumbering is called, the next Badge object created receives the number .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

While reviewing a Java inventory program, you classify its members by what they do and whether they belong to individual objects or the class. Match each category to its description.

Match each term
A.

An operation called with a current object whose state it can access directly

B.

A variable belonging to the class and shared by its objects

C.

An operation with no current object that can access object state through an explicit object

D.

A variable for which each separate object has its own stored value

Static method

Instance method

Instance field

Static field

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors