Referring to the instance variable when local variable has the same name can be achieved by using

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Local variable cannot have the same name as the function containing it

  • Article
  • 09/15/2021
  • 2 minutes to read

In this article

A Dim statement specifies a variable with the same name as the Function procedure containing it.

Error ID: BC30290

To correct this error

  • Remove the variable declaration, or change the name of the variable.

See also

  • Function Procedures

Feedback

Submit and view feedback for

There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object.

Referring to the instance variable when local variable has the same name can be achieved by using

Usage of Java this keyword

Here is given the 6 usage of java this keyword.

  1. this can be used to refer current class instance variable.
  2. this can be used to invoke current class method (implicitly)
  3. this() can be used to invoke current class constructor.
  4. this can be passed as an argument in the method call.
  5. this can be passed as argument in the constructor call.
  6. this can be used to return the current class instance from the method.

Suggestion: If you are beginner to java, lookup only three usages of this keyword.

Referring to the instance variable when local variable has the same name can be achieved by using

1) this: to refer current class instance variable

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Understanding the problem without this keyword

Let's understand the problem if we don't use this keyword by the example given below:

Test it Now

Output:

In the above example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable.

Solution of the above problem by this keyword

Test it Now

Output:

111 ankit 5000.0
112 sumit 6000.0

If local variables(formal arguments) and instance variables are different, there is no need to use this keyword like in the following program:

Program where this keyword is not required

Test it Now

Output:

111 ankit 5000.0
112 sumit 6000.0

It is better approach to use meaningful names for variables. So we use same name for instance variables and parameters in real time, and always use this keyword.


2) this: to invoke current class method

You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method. Let's see the example

Test it Now

Output:


3) this() : to invoke current class constructor

The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining.

Calling default constructor from parameterized constructor:

Test it Now

Output:

Calling parameterized constructor from default constructor:

Test it Now

Output:

Real usage of this() constructor call

The this() constructor call should be used to reuse the constructor from the constructor. It maintains the chain between the constructors i.e. it is used for constructor chaining. Let's see the example given below that displays the actual use of this keyword.

Test it Now

Output:

111 ankit java 0.0
112 sumit java 6000.0

Rule: Call to this() must be the first statement in constructor.

Test it Now

Output:

Compile Time Error: Call to this must be first statement in constructor


4) this: to pass as an argument in the method

The this keyword can also be passed as an argument in the method. It is mainly used in the event handling. Let's see the example:

Test it Now

Output:

Application of this that can be passed as an argument:

In event handling (or) in a situation where we have to provide reference of a class to another one. It is used to reuse one object in many methods.


5) this: to pass as argument in the constructor call

We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let's see the example:

Test it Now

6) this keyword can be used to return current class instance

We can return this keyword as an statement from the method. In such case, return type of the method must be the class type (non-primitive). Let's see the example:

Syntax of this that can be returned as a statement

Example of this keyword that you return as a statement from the method

Test it Now

Output:

Proving this keyword

Let's prove that this keyword refers to the current class instance variable. In this program, we are printing the reference variable and this, output of both variables are same.

Test it Now

Output:


Can local variable have same name as instance variable?

If a local variable and an instance variable have the same name, the local variable shadows or hides the name of the instance variable within the scope of the method.

What happens when the parameter has the same name as an instance variable?

There's no problem with giving parameter names and instance variable names the same name. But Java has to pick whether it is an instance variable or a parameter variable. Either way, it doesn't do what you think it should do. It doesn't initialize the instance variables with the values of the parameter variables.

What happens if the local and global variable has the same name?

If a global and a local variable with the same name are in scope, which means accessible, at the same time, your code can access only the local variable.

When a local variable and an instance variable have the same name which variable takes precedence in a method?

They're only accessible within the method they are declared inside. If there is a local variable and an instance variable with the same name, then the local variable will take precedence over the instance variable.