Write a program in java to demonstrate local variables?

Local Variable do not support default value

class Local

{

                public static void main(String ar[])

                {

                                int a;      //Local Variables

                                System.out.println(a);

                }

}
Local Variable Do Not Suppot Default Value.PNG

Output It gives error, because in java local variable of a method do not support default value and also garbage value. So it must be initializing at the time of declaration.

Local Variable 

class Local

{

                public static void main(String ar[])

                {

                                int a=7; //Local Variables

                                System.out.println(a);

                }

}
Local Variable.PNG
, , , ,

Leave a Reply