Core JavaJava Interview Questions

Difference between a local, instance and static variable in Java

Posted On
Posted By admin

Java supports three types of variables, local, instance and static. This post explains the difference between a local, instance and static variable in Java

Local Variable Instance Variable Static Variable
Defined within a method or a code block Defined outside a method at the class level Defined outside a method at the class level
Is only accessible in the method/code block where it is declared Is accessible throughout the class Is accessible throughout the class
Remains in memory as long as the method executes Remains in memory as long as the object is in memory Remains in memory as long as program executes
Does not require any special keyword Does not require any special keyword but any access specifier (private, protected or public) can be specified. Typically, private or protected is used Requires the static keyword to be specified. In addition, any access specifier (private, protected or public) can be specified. Typically, public is used
Requires to be initialized before it is used Is given default value based on its data type, so does not require to be initialized before it is used Is given default value based on its data type, so does not require to be initialized before it is used.

 

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here

Also, if you'd like to test your Java knowledge, do check out my practice tests course here


If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post