r/java • u/Alert-Neck7679 • Oct 30 '26
I think I have a nice idea for new constructor syntax
The idea is to allow setting a field / property directly from the parameter list, like this:
public class MyClass
{
int a;
int b;
int c;
public MyClass(this.a, this.b, this.c)
{
// the a, b and c fields are now holding the values that were passed as arguments
...
}
}
MyClass instance = new MyClass(1, 2, 3); // u use it like a normal constructorThese constructors can allow being body-less (like public MyClass(this.variable);), as long as they don't declare any normal parameters too.
Constructors were always a painful point for me, and even though for some cases you can just use records and skip them, a lot of times you want to add logic / allow different parameter lists / allow muttability, and then you must use normal constructors.
I would love to hear your opinions about this!