Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define default constructor for record

I have a record and want to add default constructor to it.

public record Record(int recordId) {
   public Record {

   }
}

But it created constructor with int param.

public final class Record extends java.lang.Record {
    private final int recordId;
    public Record(int);
    //other method
}

How can we add a default constructor to a record?

like image 964
Vikas Yadav Avatar asked Apr 11 '20 05:04

Vikas Yadav


People also ask

What is default constructor with example?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Can records have constructors?

If you want your record's constructor to do more than initialize its private fields, you can define a custom constructor for the record. However, unlike a class constructor, a record constructor doesn't have a formal parameter list; this is called a compact constructor.

Do I need to define a default constructor?

A default (no-argument) constructor is automatically created only when you do not define any constructor yourself. If you need two constructors, one with arguments and one without, you need to manually define both.

Is it possible to have a default constructor in a record?

A record can only have a canonical constructor. If declared the compact constructor is the canonical constructor To split hairs, you cannot ever define a default constructor, because a default constructor is generated by the compiler when there are no constructors defined, thus any defined constructor is by definition not a default one.

What is the use of default constructor in Java?

They are used to create objects, which do not have any having specific initial value. Is a default constructor automatically provided? If no constructors are explicitly declared in the class, a default constructor is provided automatically.

What is a deleted default constructor in C++?

3) Deleted default constructor: if it is selected by overload resolution, the program fails to compile. 4) Defaulted default constructor: the compiler will define the implicit default constructor even if other constructors are present. 5) Defaulted default constructor outside of class definition (the class must contain a declaration (1) ).

What is a final constructor in C++?

final (C++11) A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible . Contents.


2 Answers

To split hairs, you cannot ever define a default constructor, because a default constructor is generated by the compiler when there are no constructors defined, thus any defined constructor is by definition not a default one.

If you want a record to have a no-arg constructor, records do allow adding extra constructors or factory methods, as long as the "canonical constructor" that takes all of the record fields as arguments is called.

public record Record(int recordId) {
   public Record() {
      this(0); 
   }
}
like image 187
ThisIsNoZaku Avatar answered Oct 23 '22 21:10

ThisIsNoZaku


Explicit constructor

In your case, you can explicitly specify a no-argument constructor with the delegation to the canonical constructor with a default value if you want to and this can be done as -

public Record(){
    this(Integer.MIN_VALUE);
}

In short, any non-canonical constructor should delegate to one, and that should hold true for the data-carrying nature of these representations.

Compact Constructor

On the other hand, note that the representation you had used in your code.

public Record {}

is termed as a "compact constructor" which represents a constructor accepting all arguments and that can also be used for validating the data provided as attributes of the record. A compact constructor is an alternate way of declaring the canonical constructor.

like image 25
Naman Avatar answered Oct 23 '22 22:10

Naman