dimanche 2 décembre 2018

Immutable pattern example

I am studying the following example to learn immutable pattern :

public final class ImmutableStudent {

private final int ID;
public ImmutableStudent (int ID)
{

this.ID = ID;
}
public final int getID()
{
return ID; .
}
public static void main (String[] args)
{
ImmutableStudent student1 = new ImmutableStudent(1234);


 // error
     student.ID = 70;
    }
    }

I need to understand the use of the this keyword in the constructor. I think that use of the this keyword here is to ensure that the value passed to the constructor is assigned to the instance variable ID , not the local variable if exists in the constructor , is this right ?

Aucun commentaire:

Enregistrer un commentaire