#12 Java Scope And Lifetime Of Variables

Scope and Lifetime of Variables

The following simple programs, illustrate how to avoid programming errors by taking care where to initialize variables depending on the scope.

 class Example16

{

 public static void main(String args[]) {
 int x; // known to all code within main
 x = 10;
 if(x == 10)
 { // start new scope
 int y = 20; // known only to this block // x and y both known here.
 System.out.println("x and y: " + x + " " + y);
  x = y * 2;
} // y = 100; // Error! y not known here
// x is still known here.
 System.out.println("x is " + x);
}
}

Predicted Output:

 x and y: 10 20
 x is 40

watch This video to understand properly

Comments

Popular Posts