About Questions enthuware.ocpjp.v17.2.3691-3697

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
theujior
Posts: 11
Joined: Fri Sep 09, 2022 8:41 am
Contact:

About Questions enthuware.ocpjp.v17.2.3691-3697

Post by theujior »

Questions explain
A record declaration implicitly creates instance fields, which are private and final. These are called record "components".
JLS 8.10.1. Record Components says:
The record components of a record class, if any, are specified in the header of a record declaration.
and JLS 8.10.3. Record Members says:
For each record component, a record class has a field with the same name as the record component and the same type as the declared type of the record component. This field, which is declared implicitly, is known as a component field.

And Questions explain
Constructors and methods
1...
2...
3. If you write a constructor in a record explicitly then you must provide the canonical constructor as well.
Why?
JLS 8.10.4. Record Constructor Declarations says:
If a canonical constructor is not explicitly declared in the declaration of a record class R, then a canonical constructor r is implicitly declared in R with the following properties:...
Can't you compile a record class with non-canonical constructor and no explicitly declared canonical constructor as following?

Code: Select all

public record Student(int id, String name){
    public Student(){ //non-canonical constructor
        this(10, "");
    }
}

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Questions enthuware.ocpjp.v17.2.3691-3697

Post by admin »

1. Yes, a record component is a record field + its accessor method. The field is declared in the header.
2. You are right. The explanation is incorrect about the constructors. Must be fixed asap.

thank you for your feedback!

theujior
Posts: 11
Joined: Fri Sep 09, 2022 8:41 am
Contact:

Re: About Questions enthuware.ocpjp.v17.2.3691-3697

Post by theujior »

Again,

JLS 8.10.1. Record Components says:
The record components of a record class, if any, are specified in the header of a record declaration.
and JLS 8.10.3. Record Members says:
For each record component, a record class has a field with the same name as the record component and the same type as the declared type of the record component. This field, which is declared implicitly, is known as a component field.
So, a "component field" corresponds to a "record component". "component field" is not "record component" itself.

And "accessor method" is the same.
Furthermore, for each record component, a record class has a method with the same name as the record component and an empty formal parameter list. This method, which is declared explicitly or implicitly, is known as an accessor method.

For example, JLS 8.10.4. Record Constructor Declarations says:
The body of r initializes each component field of the record class with the corresponding formal parameter of r, in the order that record components (corresponding to the component fields) appear in the record header.

In addition, questions explain:

Code: Select all

For example:
public record Student(int id, String name) //record header
{ } //record body

This is roughly equivalent to the following class:
public final class Student extends Record
{
   private final int id; //record component
   private final String name; //record component
   public Student(int id, String name){  //canonical constructor
      this.id = id;
	  this.name = name;
   }
   
   public int id(){ return id; } //accessor
   public String name(){ return name; } //accessor
   
   //hashCode, equals, and toString methods are also provided by the compiler.
}
I think source codes should be

Code: Select all

public record Student(
    int id, //record component
    String name //record component
    ) //record header
{ } //record body

public final class Student extends Record
{
   private final int id; //component field
   private final String name; //component field
   public Student(int id, String name){  //canonical constructor
      this.id = id;
      this.name = name;
   }
   
   public int id(){ return id; } //accessor
   public String name(){ return name; } //accessor
   
   //hashCode, equals, and toString methods are also provided by the compiler.
}

admin
Site Admin
Posts: 10384
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Questions enthuware.ocpjp.v17.2.3691-3697

Post by admin »

Noted.
thank you for your feedback!

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests