constructor
The special method that runs when a new object is created from a class and sets up its starting data.
In more detail
A constructor gives the object's fields their starting values and can reject invalid input before the object exists; in Python it is the __init__ method. Passing collaborators into the constructor, rather than building them inside it, is how dependency injection works and lets tests supply doubles.
Examples
Java: a constructor has the class's name and no return type; if you write none, javac adds an empty default one.public Greeter(String name) { this.name = name; }Related terms
- class — A blueprint in code that bundles data and the functions that work on it, from which objects are made.
- object — A single thing created from a class or other type, holding its own values and able to run the type's methods.
- compact constructor — A record constructor written without a parameter list, which runs checks before the record's fields are assigned.
- dependency injection — Giving an object the collaborators it needs from outside, usually through its constructor, instead of letting it create or look them up itself.
- no-argument constructor — A constructor that takes no parameters, which JPA requires on every entity so it can create the object before filling in its fields.
- seam — A place in the code where you can swap in different behaviour without editing the code itself, such as a parameter.
- ConstructorParameters<T> — The utility type that gives a class constructor's parameter list as a tuple.
- InstanceType<T> — The utility type that gives the type of object produced by calling new on class type T.
Taught in
- The Virtual Machine, The Kit And The Runtime — Modern Java: Virtual Threads & High-Throughput Services
- The Lifecycle Of A Java Program — Modern Java: Virtual Threads & High-Throughput Services
- Designing Testable Code — Modern Java: Virtual Threads & High-Throughput Services
- Dependency Injection And The IoC Container — Modern Java: Virtual Threads & High-Throughput Services
- Dates, Times And Daylight Saving — 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
- Checked, Unchecked, Catch And Finally — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Try-With-Resources And Suppressed Exceptions — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lambdas, Functional Interfaces And Method References — 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
- Console And File I/O With Streams — 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
- Locales, Resource Bundles And Formatting — 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
- Introducing pytest — Automated Testing, TDD & Quality Engineering
- What Test Doubles Are For — Automated Testing, TDD & Quality Engineering
- Fakes, Stubs, Mocks And Spies — Automated Testing, TDD & Quality Engineering
- Why TypeScript: Contracts, Erased Types And Soundness — Production TypeScript
- Primitive Types: String, Number, Boolean, Bigint And Symbol — Production TypeScript
- The in Operator And Instanceof Narrowing — Production TypeScript
- Function Utilities: ReturnType, Parameters And Constructor — Production TypeScript
- ECMAScript Modules, CJS Interop And verbatimModuleSyntax — Production TypeScript
References
- Java Tutorials: Providing Constructors for Your Classes
- Java Language Specification, chapter 8: Classes
- Python tutorial: class objects
- MDN: constructor
- TypeScript Handbook: Classes