Difference between Class, Instance and Local variables in Java? Example (original) (raw)
There are a lot of differences between instance variable, class variable, and local variable in Java, and knowing them will help you to write correct and bug-free Java programs. Java is a full-featured programming language and provides different kinds of variables like static variables also called Class variable since it belongs to whole Class, non-static also called instance variable and local variables which vary in scope and value. Thank god Java doesn't have any register variable or auto scope like C Programming language, otherwise it would have so much detail to remember. static variables are a common source of error in may multi-threaded java program and does require a bit of carefulness while using it. On the other hand instance, the variable and the local variable have less sharing visibility than a static variable.
Instance Variable vs Class Variable in Java
let's first see the difference between instance variable and class variable also known as non-static vs static variable in java. Instance variables are per instance (object) basis. If you have 5 instances of one class you will have five copies of an instance variable.
These are also referred to as non-static variables and initialized when you create an instance of any object using the new() operator or by using other methods like reflection like Class.newInstance().
Here is an example of class, instance, and local variable in Java:
import static System.out.*; public class Variables{ private static int iAmClassVariable; private int iAmInstanceVariable;
public static void main(String args[]){ int iAmLocalVaraible = 3; println("class variable: " + iAmClassVariable); println("instance variable: " + iAmInstanceVariable) println("local variable: " + iAmLocalVaraible) }
On the other hand, Class variables are declared using static keywords and they have the exact same value for every instance. static or class variables are initialized when the class is first loaded into JVM memory, unlike instance variables which are initialized when an instance is created. You can also see these Java programming courses to learn more about Java's fundamental concepts like this.
Static variables are similar to a global variable in C and can be used to store data which is static in nature and has the same value for all instance, but at same static variable also cause subtle concurrency bugs if updated by multiple threads. you can read more about static keywords in my post secrets of static keywords in Java.
Instance variable vs local variable in Java
Now let's see the difference between the instance variable and the local variable. local variables are local in scope and they are not visible or accessible outside their scope which is determined by {} while instance variables are visible on all parts of code based on their access modifier like public, private, or protected.
The only public can be accessed from outside while protected and private can be accessed from subclass and class itself. Access modifiers can not be applied to a local variable and you can not even make them static.
The only modifier which is applicable to a local variable is final and only final local variables are visible inside the anonymous class. the value of the instance variable is limited to an instance, one instance can not access the value of another instance in Java. If you want to learn more bout variables in Java, then you can also check out these free Java Programming courses to start with.
How to use local, instance, and static variables in Java
It's good to know some of the best practices related to declaration and use of Variables in Java while learning differences among different types of variables in Java:
1. Always name your variable as per the Java Bean naming convention.
2. By default give private access to your member variables (both static and instance) and provide more access step by step e.g. from private to protected to package to the public. This way you will be following the encapsulation principle.
3. Always declare a local variable where you use instead of declaring it on top of the method or block.
4.Don't hide instance or static variables by giving the same name to a local variable. this may result in subtle programming bugs.
5. Be consistent with your variable naming convention don't mix different conventions from different languages e.g. some programming languages use the first word to denote the type of variable like bExit to denote boolean Exit variable or iNumber to denote integer Number variable. T