Tuesday, July 7, 2009

Variables and Scope

Variables are of 2 types:
  • primitive type
  • reference type
Variables defined within a method are local variables,but are sometimes refered as automatic,temporary or stack variables.You must initialise local variable explicitly before the first use.

Method parameters and constructor parameters are also local variables but they are initialised by the calling method.

Variables defined outside the method are created when the object is created with new xxx() call.There are 2 possible types of this variables.
  • The first kind is declared using the static keyword.
Variables labeled static are created when the class is loaded and continues to exists as long as the class is loaded.

  • The second variable is instance variable that is declared without the static keyword.
Instance variable continues to exists as long as object exists.Instance variable are sometimes referred to as the member variables,because they are the member of the object created from the class

Method parameter variables define argument passed in a method call.

Local VARIABLES are created when execution enters the method and are destroyed when the method is exited.That is why variables are sometimes referred as temporary or automatic.

No comments: