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

2011年7月14日Posted byIndika

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 结构化编程和Obje之间的区别ct Oriented Programming Difference Between Object and Instance Difference Between Inheritance and Composition

Filed Under:ProgrammingTagged 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 ReplyCancel 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 Can and Could

Difference Between Can and Could

Difference Between Chamilia Bracelets and Pandora Bracelets

Difference Between Photon and Quantum

Difference Between Photon and Quantum

Difference Between Blackberry Bold 9900 and Samsung Galaxy Pro

Difference Between Representative and Transition Elements

Difference Between Representative and Transition Elements

Latest Posts

  • Vitam之间的区别是什么ins and Amino Acids
  • What is the Difference Between Induction Cooker and Normal Cooker
  • What is the Difference Between Vitiligo and Psoriasis
  • What is the Difference Between Cancellous and Cortical Bone
  • What is the Difference Between Electric and Induction Cooktop
  • What is the Difference Between Prevention and Cure
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

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