Instance Members and Static Class Members

A student name belongs to one object; a shared count does not

Each student registration has its own name. A class-level example also counts how many instances have been constructed. These values have different owners.

An instance member belongs to one object. A JavaScript static member belongs to the class constructor itself and is accessed through the class rather than an ordinary instance.

  1. Locate ownership

    Distinguish per-student data from shared class state.

  2. Call the right operation

    Use instance methods on objects and static methods on the class.

  3. Understand shared lifetime

    Recognize that constructed-count is not a live enrollment total.

Two individual names, one shared counter

  1. First instance

    Store Maya on one object and increment the class count.

    Maya; count 1
  2. Second instance

    Store Ravi on another object.

    Ravi; count 2
  3. Class operation

    Read the shared construction count.

    Registration.count → 2
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

Static means class-owned, not permanently fixed

this.name inside a constructor initializes the new instance. Registration.count names a property on the class. A static field can still change if the program assigns to it.

A static method can represent a class-related utility or factory. It does not automatically have one student instance’s state; supply data or operate on class-owned information deliberately.

Before starting, review separate instances. These examples use JavaScript; other languages can use different syntax and rules.

Read a name and the shared construction count

The constructor stores each name and increments the class field. Calling maya.label uses Maya’s instance; calling Registration.totalCreated uses class state.

The example counts creations only. Removing an object reference does not decrement the counter, and no destructor keeps it synchronized with actual enrollment.

JavaScript · local teaching example

class Registration {
  static count = 0;
  constructor(name) {
    this.name = name;
    Registration.count += 1;
  }
  label() { return this.name; }
  static totalCreated() { return Registration.count; }
}
const maya = new Registration('Maya');
new Registration('Ravi');
console.log(maya.label());
console.log(Registration.totalCreated());

Expected output

Maya
2

A static calculation can take explicit inputs

This utility does not depend on one student object. The class method receives marks and returns their sum.

A plain function would also be reasonable here. Use a static method when grouping it with the class clarifies ownership rather than merely adding ceremony.

JavaScript · local teaching example

class Marks {
  static total(maths, science) { return maths + science; }
}
console.log(Marks.total(40, 35));

Expected output

75

Apply the rule to a small change

Create three registrations and display the first name and the creation count. Keep the shared field on the class.

Worked answer

The first object still owns Maya’s name, while the class count reaches three. Do not copy the count into each instance and assume those copies stay synchronized.

JavaScript · local teaching example

class Registration {
  static count = 0;
  constructor(name) { this.name = name; Registration.count += 1; }
}
const first = new Registration('Maya');
new Registration('Ravi');
new Registration('Asha');
console.log(first.name);
console.log(Registration.count);

Expected output

Maya
3

Recognize mistakes and limits

Shared static state can persist across tests or requests in the same process. Reset or inject the relevant state deliberately rather than depending on execution order.

Python class attributes and other languages’ static fields have their own lookup and shadowing rules. In this JavaScript example, writing an instance property named count would not automatically update Registration.count.

For the language or tool rules used here, see JavaScript class behavior.

Check the core idea

Which expression reads the shared class count?
Choose one answer. You can try again.

Answer and explanation

Registration.count

Yes. The count is stored on the class.

Apply the idea to a new situation

Does count equal current enrolled students after objects are discarded?
Choose one answer. You can try again.

Answer and explanation

No; it records creations in this example, not current enrollment.

Yes. State meaning depends on the implemented update rules.

Say who owns each member and what it measures

Instance state belongs to an individual object; static members belong to the class. Shared state needs a defined lifetime and update policy, and utility methods need explicit inputs.

Connect this idea with shared name scopes and isolated tests.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Object-Oriented Design: Focused Responsibilities →