When must a program explicitly use the this reference?
Group of answer choices:
O When accessing a private variable
O When accessing a public variable
O When accessing a local variable in a method
O When accessing an instance variable in a method that has the same name as a method parameter

Respuesta :

A program explicitly use the reference when accessing an instance variable that is shadowed by a local variable.

Explanation:

A local variable is a variable that is defined locally within the function. It cannot be accessed outside the function. The variables that are declared in the function can be used only within that function.

The local variables can be created with the same name in different functions. A program can explicitly use the reference when the field is shadowed by the local variable.

An instance variable is similar to class variable and it is defined in a class.

When accessing a local variable in a method program explicitly use the reference.

Explanation:

  • When we declare a variable as a reference,it is used an alternative name for that variable
  • By putting '& ' in the declaration  we can tell that the variable is used as a reference.
  • If a function receives a reference to a variable, it can modify the value of the variable.

  • There are certain limitation of a reference-Once it  is used,it cannot be later used  to reference another object; it cannot be restated.
  • The value of a references cannot be NULL. Initialization of a reference is required when it is declared .

we can use both  references and pointers to change the local variables of one function inside another function. Reference and pointer can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain.