Programming





What are the differences between local variable, instance variable and static variable?

What are the differences between local variable, instance variable and static variable?


by Administrator | 25-Mar-2020
Java

Tags:  Core Java  Instance Variable  Local Variable  Static Variable

Share: 


Characteristic

Local variable

Instance variable

Class variable

Declaration

In a method, constructor, or block

In a class, but outside a method

In a class, but outside a method. Must be declared static

Use

Local variables hold values used in computations in a method.

Instance variables hold values that must be referenced by more than one method (for example, components that hold values like text fields, variables that control drawing, etc), or that are essential parts of an object's state that must exist from one method invocation to another.

 

Class variables are mostly used for constants or variables that never change from their initial value.

Lifetime

Created when a method or constructor is entered.

Destroyed on exit.

Created when the instance of the class is created with new.

Destroyed when there are no more references to the enclosing object (available for garbage collection).

Created when the program starts.

Destroyed when the program stops.

Scope/Visibility

Local variables (including formal parameters) are visible only inside a method, constructor, or block in which they are declared. Access modifiers like private, public, etc cannot be used with local variables. All local variables are effectively private to the block in which they are declared. No part of the program outside of the method/block can access them. A special case is that local variables declared in the initialize part of a for loop have a scope within for statement.

Instance (field) variables can be seen by all methods in the class. Which other classes can see them is determined by their declared access.

private should be your default choice in declaring them. No other class can see private instance variables. This is regarded as the best choice. Define getter and setter methods if the value has to access or set from outside so that data consistency can be enforced, and to preserve internal representation flexibility.

Default (also called package visibility) allows a variable to be seen by any class in the same package. Private is preferable.

Public variables can be seen from any class and generally a bad idea.

protected variables are only visible from any descendant classes. Uncommon, and probably a bad choice.

Same as an instance variable, but are often declared public to make constants available to users of the class.

Declaration

Declared before use anywhere in a method or block.

Declared anywhere at class level (before or after use).

Declare anywhere at the class level with static.

Default value

None. Must be assigned a value before the first use.

Zero for numbers, false for boolean, or null for object references. May be assigned value at the declaration or in the constructor.

Same as an instance variable and in special static initializer block.

Access from outside

Impossible. Local variable names are known only within the method.

Instance variables should be declared private to promote information hiding so should not be accessed from outside a class. However, in the few cases where there are accessed from outside the class, they must be qualified by an object (eg, myPoint.x).

Class variables are qualified with the class name (eg, Color.BLUE). They can also be qualified with an object, but this is a deceptive style.

Name syntax

Standard rules.

Standard rules, but are often prefixed to clarify difference from local variables, eg with my, m, or m_ (for member) as in myLength, or this as in this.length.

static public final variables (constants) are all uppercase, otherwise normal naming conventions. Alternatively, prefix the variable with "c_" (for class) or something similar.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.