liveBook · Manning

Static field

Also called static variable, class variable, class attribute

A static field is a named variable associated with a class or program scope, rather than stored separately for each object instance.

How 7 Manning books explain it

The Well-Grounded Rubyist, Fourth Edition

A class variable is a Ruby variable whose information is shared per class hierarchy and whose name begins with two at-signs.

How it is used
Uses class variables to share state among a class, its methods, its instances, and its subclass hierarchy.
How deep it goes
Works through syntax, visibility, a counting implementation, inheritance behavior, and a class-instance-variable alternative; does not descend into runtime internals.
Examples
Tracks registered car makes and counts, then contrasts a shared subclass counter with a class-instance-variable counter.
What you can do after
Build it from scratch: implement shared class-hierarchy state and choose separate class-object state when subclass totals must differ.

Read how The Well-Grounded Rubyist, Fourth Edition explains static field →

The Well-Grounded Python Developer: How the pros use Python and Flask

A class variable is an attribute stored on a Python class and shared by its instances unless shadowed.

How it is used
Keeps a page-visit count accessible across instances created for separate web requests.
How deep it goes
Shows a working class-level counter and its single-server limit, without developing a multiple-server solution.
Examples
Uses PageVisit.COUNT and counts() to increment and display a page-visit counter.
What you can do after
Build it from scratch: implement a class-level page counter for requests handled by one server.

Read how The Well-Grounded Python Developer: How the pros use Python and Flask explains static field →

The Quick Python Book, Fourth Edition

A class variable is an attribute defined on a class and shared through that class's inheritance structure unless shadowed or otherwise restricted.

How it is used
Uses class variables for shared class-level values, instance defaults, and collections of instances.
How deep it goes
Explains declaration, lookup, assignment shadowing, inheritance, and private names with worked examples.
Examples
Uses a Circle pi value and instance list, plus a superclass variable shadowed by a subclass and instance.
What you can do after
Apply Python class variables to shared values and diagnose instance or subclass assignments that shadow them.

Read how The Quick Python Book, Fourth Edition explains static field →

Python How-To

A class attribute is an attribute stored on a Python class and available to instances through attribute lookup when no instance-specific value overrides it.

How it is used
Shares values or resources across instances when per-instance storage would duplicate data.
How deep it goes
Defines placement, memory rationale, and practical uses, but stops short of detailed attribute-lookup internals.
Examples
Uses shared task users, SQLite connections, and Direction constants to demonstrate appropriate and inappropriate class-attribute designs.
What you can do after
Apply class attributes to reduce duplicated shared state and recognize when Enum is preferable for named constants.

Read how Python How-To explains static field →

100 Java Mistakes and How to Avoid Them

A static field is a Java class variable whose storage is associated with the class rather than with each object instance.

How it is used
Warns against confusing class-wide state with per-object state during refactoring, review, testing, and concurrent use.
How deep it goes
Explains sharing, sequential-test masking, mutable static-final state, concurrency consequences, and static-analysis detection.
Examples
Uses a shared Person name and a static final TreeWalker queue to demonstrate cross-object and cross-thread state leakage.
What you can do after
Diagnose/debug with code review, concurrency-focused tests, IDE highlighting, and static-analysis inspections.

Read how 100 Java Mistakes and How to Avoid Them explains static field →

Learn Rust in a Month of Lunches

A Rust program item representing a value stored at a fixed memory location for the entire program lifetime.

How it is used
Provides program-wide values that remain available for the entire program lifetime.
How deep it goes
Explains declaration syntax, fixed storage, lifetime, compile-time limits, and unsafe mutable access with a concurrency failure example.
Examples
Uses global month and season values, then contrasts a small successful counter with a large multithreaded static mut counter.
What you can do after
Apply static declarations for global values and diagnose why mutable static access requires unsafe code.

Read how Learn Rust in a Month of Lunches explains static field →

Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond

A property defined on a class or constructor itself rather than on instances created from it.

How it is used
Uses class-level values for settings shared across instances and for configuring sprite appearance.
How deep it goes
Defines storage on the constructor rather than instances or prototype, shows assignment-style desugaring, and stops short of deeper mechanics.
Examples
Demonstrates a DataStore domain, a bicycle wheel count, and shared sprite styling contrasted with unique comet shapes.
What you can do after
Apply static fields to place shared configuration and appearance values on classes, while keeping per-object variation on instances.

Read how Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond explains static field →

Also covered in

What to know first

Related concepts

Questions about static field

What is static field?

A static field is a named variable associated with a class or program scope, rather than stored separately for each object instance.

Which Manning books explain static field?

The Well-Grounded Rubyist, Fourth Edition, The Well-Grounded Python Developer: How the pros use Python and Flask, The Quick Python Book, Fourth Edition, Python How-To, 100 Java Mistakes and How to Avoid Them, Learn Rust in a Month of Lunches and Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond.

What should I know before learning static field?

Manning books that teach static field usually introduce object, thread, constant and class first.

What is the difference between static field and constant?

A static field is a named variable associated with a class or program scope, rather than stored separately for each object instance. A constant is a value or named binding treated as fixed rather than reassigned or modified during a computation, whether this is enforced by a language or followed by convention.

What kinds of static field are there?

Kinds of static field include method.

What is static field a kind of?

Static field is a kind of attribute. An attribute is a named property or piece of information associated with an entity, object, or structured data item.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.