28 February 2022

Understanding variable scope in Java

Welcome to another tutorial on the "How to code in Java" tutorial series. In the last tutorial, we've gone through how pass by value and pass-by-reference works in Java. In this tutorial, we'll be going through how variable scope works in Java. This would be a rather short article, so sit back and enjoy. 



Since Java programs are organized in classes, this makes two possible variable scopes for the program; Member variables of a class and the local variable in a method.


Member variables (Class level)


The variables declared in a class can be accessed throughout the class's methods.

Demonstrating class member variables scope


The output for the code above.

As we can see in the code above, the class was able to access and use the values of the variables. Although it can be accessed by its own object, how about other classes that want to access the values of the variables? Well, if you've been following the series, then you'll understand how the access modifier concept is applied here. If you haven't read the article yet, I encourage you to read through it first before continuing.

Here's a simple example of accessing a variable with the private access modifier in another class. As we can see, it caused a compilation error indicating that we're trying to access class fields that are private to other classes. 

The class member variables have scope depending on their access modifier.

Well, the private class field is not accessible.

Based on the access modifier of the variables in their class, they will have their respective scope bound to their declaration. For example, for a member variable with a private access modifier, its scope is only within its class methods. While a member variable with a public access modifier, its scope is limited by the type of declaration it is declared. If the variable is declared as a static member variable, then it can be used everywhere without initializing an object for it. You can read more on static and non-static methods and variables/fields in another article of the series.


Local Variables (methods level)


Now, the next category is the local variable. A local variable is commonly referred to as the variables declared inside a method. The lambda and callback methods also have their own scope. For example, using this code below, we can use the variable that's declared locally in the lambda function of the forEach function for the ArrayList. Still, if we were to try and use that variable outside of that lambda function, the compiler will return an error when compiled. 

Demonstrating local variable scope with a lambda function


Output for the code


Trying to access the local variable that's declared inside the lambda function.


Get an error during compilation.

That's about all I could tell you regarding variables scopes. I hope you now know the scope of the variable you declared in your program and where you can access them. If you have any questions, suggestions, or critics, feel free to leave a comment in the comment section below. In the following tutorial, we'll finally be looking into our two OOP concepts, Abstraction and Encapsulation. So stay tuned!

No comments:

Post a Comment