field
A variable declared inside a class that stores part of an object's state.
In more detail
Each object usually gets its own copy of a field, while class-level fields are shared. Fields are commonly kept private so other code must go through methods.
Examples
.NET binders and serialisers ignore public fields and use properties instead.Java: private final String name; a final field is assigned once, in the constructor.private final String name;Related terms
- property — A named value belonging to an object; in languages such as C# and Python, it can also be a member that looks like a field but runs code when read or written.
- readonly — A modifier, in languages such as C# and TypeScript, that lets a field or property be assigned only when it is initialised, never afterwards.
- primary constructor — Constructor parameters written straight after a class name, as in class ProductService(IProductRepository repository), which stay in scope for the whole class body.
- class — A blueprint in code that bundles data and the functions that work on it, from which objects are made.
- final — A Java keyword meaning cannot be changed further: a final field is assigned once, and a final class cannot be extended.
- constructor — The special method that runs when a new object is created from a class and sets up its starting data.
- static — A keyword marking a field or method as belonging to the class itself, so it can be used without creating an object.
Taught in
- The C# Language: Types, Variables, and Flow Control — Modern .NET Core, C# & Enterprise Microservices
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- The Lifecycle Of A Java Program — Modern Java: Virtual Threads & High-Throughput Services
- Records And Data Classes — Modern Java: Virtual Threads & High-Throughput Services
- Objects, Nested Classes And Garbage Collection — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Classes, Records And Initialisation Order — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Scope, var And Immutable Objects — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Inheritance, Overriding And Object's Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Interfaces: Default, Static And Private Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Arrays: Declare, Sort, Search, Compare — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Modules: Exports, Opens And Services — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Thread Safety: Locks And Atomics — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Serialization: What Survives The Trip — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: I/O And Localization — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Mock Exam: Sit It, Score It — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
References
- Fields (C# programming guide)
- Java Tutorials: Declaring Member Variables
- Java Language Specification, chapter 8: Classes