variable scope in java | java variable scope

 

variable scope in java

Variable scope in java

The Scope of a variable refers to its visibility. Sometimes, a method needs to refer to the object we are currently using. Java added the reserved word ‘this’ for this task.

‘this’ is an operator that allows us to access the current object (the class being used), and among other things, it will help us access the properties or methods of the class. Using "this", we can make the parameters received in the method different from the properties of the class.

 public class WithThis

{

    int x, y;

    // Parameterized constructor

    WithThis(int x, int y)

    {

        this.x = x;

        this.y = y;

    }

    void display()

    {

        //Displaying value of variables a and b

        System.out.println("x = " + x + "  y = " + y);

    }

    public static void main(String[] args)

    {

        WithThis ob = new WithThis(40, 60);

        ob.display();

    }

}

As you can see from the code above, we have a code whose properties are named x and y, and the constructor of this class also receives two parameters called x and y. In order to make these two variables (the attributes of the class and the parameters received in the method) different, we can use the word "this" as follows:

We use the operator this

WithThis (int x, int y)

{

this.x = x;

this.y = y;

}

 As we can see, to make a difference between the values received in the Constructor and the attributes of the class, we can use the ‘this’ operator.

This is just an example of using the "this" operator, but basically it allows us to access the properties and methods of the current object being used. First of all, this can be confusing because we are using the code of the class (ie the template) and this code will only be executed before the object is created and this constructor is called​​. We send parameters, and the constructor receives and processes them. Therefore, we must be accustomed to thinking about two moments, namely creating a class or template and creating an object, which will happen when the code we programmed in the class or template is actually executed.

Although the use of the 'this' operator will sometimes seem redundant, it is a good practice to use it to refer to the attributes of the class in which we are working, since by reading our code we will quickly recognize which variables are attributes of a class and which are not. They are. Within the constructors or methods of a class, the ‘this’ operator will always reference the object that was invoked. If we look at the code, both the received argument and the class attribute are called exactly the same, so the argument about the class attribute takes precedence, this is known as concealment of the class attribute. And to solve this problem, it is enough to use the ‘this’ operator before the variable, just as if we were accessing the attribute of a class by means of the dot operator.

Depending on where the variable is defined, it will be the duration of the variable, and this is known as the Scope of a Variable. If we define a variable as an attribute of a class, this variable is known as a Class variable, and it will exist for as long as the object exists in memory.

These variables will automatically be initialized with their default values. For example, the value of integer type will be initialized to 0, the variable type of Boolean type will be initialized to false, and the Object type of Boolean value will be initialized to null. On the other hand, local variables are any variables defined in the method, including the parameters received by the function.

These variables will automatically be initialized with their default values. For example, the value of integer type will be initialized to 0, the variable type of Boolean type will be initialized to false, and the Object type of Boolean value will be initialized to null. On the other hand, local variables are any variables defined in the method, including the parameters received by the function.

More Java program


SHARE THIS
Previous Post
Next Post