The following small Java application shows blocks of statements. The
variables t
are all refering to different objects. After
the last declaration and initialization of the variables t
it
cannot be introduced anew in the main
method. The variable
r
exists and can be used at every location in the
main
method.
When you run this program you will see on your computer screen.public class blockTest { public static void main(String[] args) { int r; // declaration of integer r { int t=2; // introduction of temporary integer t r = 3*t; System.out.println("t = "+t+", 3*t = "+r); }; { int t=4; // introduction of new temporary integer t r = 5*t; System.out.println("t = " + t + ", 5*t = " + r); }; int t = 6; // introduction of integer t in the // rest of the main method r = 7*t; System.out.println("t = " + t + ", 5*t = " + r); } }
t = 2, 3*t = 6 t = 4, 5*t = 20 t = 6, 7*t = 42