Why can't a default constructor have parameters in EK9?
← Classes and OOP · Ref: Q776
The 'default' keyword on a constructor means 'compiler, generate a zero-argument constructor that initialises all fields to their declared values'. Adding parameters contradicts this — the compiler cannot know how to map parameters to fields.
DEFAULT CONSTRUCTOR RULES
- 'default ClassName()' — no parameters, compiler generates body
- All fields must be initialised at declaration for default to work
- You can have BOTH a default constructor AND parameterised constructors
THIS EXAMPLE
The Config class has two fields initialised at declaration and uses 'default Config()' for zero-argument construction. It also has a parameterised constructor for custom values. Both coexist.
WHEN TO USE DEFAULT
Use default when all fields have sensible initial values and you want a zero-argument constructor without writing the body. Use a regular constructor when you need parameters.
See Q89 for constructor basics. See Q82 for default operators. See Q774 for abstract constructor rules.
Example
defines module qa.classesandoop.defaultconstructor defines class Config name <- "default" timeout <- 30 default Config() Config() -> n as String t as Integer name :=: n timeout :=: t describe() <- rtn as String: `${name} timeout=${timeout}` default operator ? Setting label <- "none" enabled <- false default private Setting() Setting() -> l as String label :=: l enabled: true default operator ? defines program ShowConfig() stdout <- Stdout() defaults <- Config() custom <- Config("prod", 60) if defaults? stdout.println(defaults.describe()) if custom? stdout.println(custom.describe())
Common mistakes
E07080 — Adding a parameter to a default constructor is invalid. The 'default' keyword means zero-argument auto-generated constructor. Either remove 'default' and write a parameterised constructor, or remove the parameter. See ek9 -h E07080 for details.
Incorrect:
default Config() -> name as String
Correct:
default Config()
E07080 — Even with the private modifier, a default constructor cannot have parameters. Default means zero-arg. Remove 'default' to create a parameterised private constructor. See ek9 -h E07080 for details.
Incorrect:
default private Setting() -> initial as Integer
Correct:
default private Setting()
Other ways to ask this
- What triggers E07080 invalid default constructor?
- What does the default keyword mean on a constructor?
- How do I auto-generate a constructor in EK9?
Coming from another language?
Java: default constructor is implicit (no-arg, compiler-generated if no constructors defined). Python: __init__ with defaults provides similar pattern. Rust: Default trait derive provides zero-arg construction. Kotlin: default parameter values on primary constructor. Go: zero-value initialization is automatic. EK9: explicit 'default ClassName()' keyword, must be zero-arg, fields must be initialised.
Keywords: E07080, initialise, parameters, default, zero-arg, generate, constructor, class, fields