Types of variables

local variables

  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are visible only within the declared method, constructor or block.

Example…Screen Shot 2016-07-24 at 2.42.25 PM

instance variables

instance

  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • The instance variables are visible for all methods, constructors and block in the class.

Example…Screen Shot 2016-07-24 at 2.43.22 PM

Class / Static variables

static

  • Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
  • Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.
  • Static variables can be accessed by calling with the class nameClassName.VariableName.

Example…Screen Shot 2016-07-24 at 2.44.22 PM

 

Leave A Comment

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