Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Instance Variable and Local Variable

Difference Between Instance Variable and Local Variable

July 14, 2011 Posted by Indika

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.

Related posts:

Difference Between Object Oriented Programming and Procedural Programming Difference Between Encapsulation and Abstraction Difference Between Structured Programming and Object Oriented Programming Difference Between Object and Instance Difference Between Inheritance and Composition

Filed Under: Programming Tagged With: Class, code block, field variables, heap, instance variable, local variable, member variables, Object Oriented Programming, variable, variable in OOP

About the Author: Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Inbreeding and Outbreeding

Difference Between Inbreeding and Outbreeding

Difference Between Ethanol Fermentation and Lactic Acid Fermentation

Difference Between Ethanol Fermentation and Lactic Acid Fermentation

Difference Between Osteoporosis and Osteomalacia

Difference Between Tap Root and Fibrous Root

Difference Between Tap Root and Fibrous Root

Difference Between Knocking and Detonation

Difference Between Knocking and Detonation

Latest Posts

  • What is the Difference Between Oxybenzone and Avobenzone
  • What is the Difference Between Flakiness Index and Elongation Index
  • What is the Difference Between Salmonella and Shigella
  • What is the Difference Between NTU and FTU
  • What is the Difference Between Escherichia coli and Entamoeba coli
  • What is the Difference Between Education and Socialization
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.