Instance Variable vs Local Variable
An instance variable is a type of variable that is present in object oriented programming. It is a variable that is defined in a class, and each object of that class holds a separate copy of that variable. On the other hand, the use of local variables is not limited to object oriented programming languages. It is a variable that can be assessed only within a particular block of code (e.g. function, loop block, etc.) in which it is defined. Due to this reason, local variables are said to have a local scope.
What is an Instance Variable?
Instance variables are used in object oriented programming for storing the state of each object in a class. They are also known as member variables or field variables. Instance variables are declared without using the static keyword in Java. The values stored in instance variables are unique to each object (each object has a separate copy), and the values stored in them represent the state of that object. Space for an instance variable is allocated in the heap, when that object is allocated in the heap. Therefore, instance variables are kept in the memory as long as the object is live. For example, color of one car is independent from the color of another car. So the color of a car object can be stored in an instance variable. In practice, instance variables are declared inside classes, and outside methods. Usually, instance variables are declared as private so that, they could be accessed only within the class it is declared.
What is a Local Variable?
Local variables are variables having a local scope, and they are declared within a specific code block. Local variables can be seen as variables that are used by a method to store its temporary state. Scope of a local variable is determined using the location that the variable is declared, and special keywords are not used for this purpose. Typically, access to a local variable is limited within the code block that it is declared (i.e. between the opening and closing braces of that code block). Local variables are typically stored in the call stack. This would allow recursive function calls to maintain their own copies of the local variables to be stored in separate memory address spaces. When the method finishes its execution, information about that method is popped out from the call stack, also destroying the local variables that were stored.
What is the difference between Instance Variable and Local Variable?
Instance variables are declared within classes outside methods, and they store the state of an object, while local variables are declared within code blocks, and they are used for storing the state of a method. An instance variable is live as long as the object that contains that variable is live, while a local variable is live during the execution of that method/ code block. An instance variable (that is declared public) can be accessed within the class, whereas a local variable can only be accessed within the code block that it is declared. Usage of instance variables is only limited to object oriented programming, while local variables do not have such a limitation.
Leave a Reply