Massimo Caliman
by Massimo Caliman
2 min read

Languages

  • English

Categories

  • Java

Tags

  • Java
  • Programming Languages

It is possible to find two situations of interest related to the use of methods, the careful handling of which allows for improved the efficiency of the code:

  • homonymy of the parameters of the methods and variables of the class in which the methods are defined
  • multiplicity of constructors of the class

The first situation arises from the fact that a typical use of methods consists of providing values to be assigned to object variables. In this case, it is evident that the method parameters and the class member data possess the same semantics and therefore it is not reasonable to identify one and the other with different names.

The problem that follows is that of being able to discriminate between different entities that are identified with the same identifier.

The second situation arises from the function of constructors, the purpose of which is to provide different procedures for initialisation of objects that may depend on particular application requirements.

Typically, a class provides a constructor with default values for object variables and one or more other constructors for specific initialisations.

It is reasonable to expect that the presence of multiple constructors, similar in their functionality, will result in duplication of part of the code or these methods with consequent waste of memory.

In order to deal with the situations indicated, the Java programming language provides the programmer with the reserved word this.

this is a keyword that can be exploited under two distinct interpretations:

  • reference to an object
  • reference to another constructor

In the first meaning, the keyword this is exploited in the resolution of homonyms between the parameter of a method and the name of an instance variable:

class T {
   float car;
   void m(float var){
       this.var=var;
   }
}

It forces the compiler to interpret the specification this.var as a reference to the var instance variable of the object in which the m() method is activated. Thus, in the this.var=var command, the var identifier to the right of the assignment operator refers to the method parameter, while the specification this.var to the left of the same operator refers to the attribute of the object.

The second meaning of the keyword this relates to the presence of several constructors. In order to reduce the code produced by the compiler, the constructors of a class may be broken down into a primary constructor and one or more secondary constructors. Whilst the primary constructor is itself coded, the secondary constructors refer to the primary constructor by returning its parameters callback parameters in the this specification. The calling specification must be the first statement that appears in the code of the secondary constructors.

class T {
   float var;
   public T(float var){
      this.var=var;
   }
   public T(){
      this(0.03);
   }
}