Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Local and Global Variables

Local Variable are variables defined with in block like


with in method -> sum(int I,int j) { int res = i+j ;} or in
control statements -> if() { var = value } or in Loop ->
while(true) { var = value; } or in try catch -> try { var
=value } .. Life of local variable is limited to block only
and cannot be used out side block.

Note :
1. Arguments passed to methods are also local in
scope.
2. It is compulsory to initialize local variable with initial
value.
Global Variables are variables defined out side block or
with in class outside method and there scope is
available for complete class , they can be acsessed any
where with in class.

There are Two ways to use local variable of one method


in another method :-

1. Return local variable and pass it as argument to


next method.
2. Assign Local variable to Global variable and then
use Global variable in next method.
Instance and Class Members

Instance members are non static methods and variables


of class, they are initialized at run time after calling of
constructor and are acsessed only by class reference
variable only (Cannot be called directly by class name)

Multiple instances of class create separate memory of


each instances which are not shared among them
selves hence each instance holds separate private
value.

Example

public class Abc


{
int i = 0; // non static variable
public static void main(String[] args)
{
Abc a = new Abc();
a.i =10;
System.out.println(a.i); // show 10

Abc b = new Abc();


System.out.println(b.i); // show 0 not 10

// two constructors create two memories which hold


separate value of variable i
}
}

Class members are static methods and variables which


are bound with class at compile time and can be called
directly by class name with out constructor. Multiple
instances of class share static members among them
selves , if any instance changes value of static variable it
remains changed for complete class and other instances
will get changed value.

Example

public class Abc


{
static int i = 0; // static variable
public static void main(String[] args)
{
Abc a = new Abc();
a.i =10;
System.out.println(a.i); // show 10
Abc b = new Abc();
System.out.println(b.i); // show 10 not 0

// two constructors share value of variable i


}
}

Some points of static key word


1. Outer class cannot be static , but inner class can be
static.
2. Constructor cannot be static.
3. Local variables cannot be static , only global
variables can be static.
4. Static methods can call static members only.
5. Non static methods and constructor can call both
static and non-static members.
6. Static methods / variable can be called directly by
class name. (they can also be called by class ref
variable)
7. Non static methods and variables can only be
called by class ref variable after constructor.
8. To override static methods in child class both parent
and child methods must be static.
9. Static block is called first in program , even before
main()

You might also like