Variables in Java

A variable is the name that reserved space in memory . Variables in java are three types .
1. Local Variable :
           a) A variable which is declared inside the method of a class
           b) Local variable can be access within the method not outside the method.
           c) There is no default value for local variable . That means it should be
               declared and initialize before first use .
           d) Access  modifiers can not be used for local variable .
2. Instance Variable :
          a) A variable which is declared inside the class but outside the method.
          b) Instance variables are created  when a object is created of a class using
             " new " keyword .
          c) Access  modifiers can  be used for Instance variable .
          d) Instance variable has default value . Like boolean is false , int is '0'
3. Static Variable :
          a) A variable which is declared as static is called static variable .
          b) static variable can be access by calling the class name .
               Example :
                        cllassname.staticvariablename

Example :

   class First{ 

             //instance variable

           int y=500;

             //static variable

           static int z=200; 

         void display(){ 

             //local variable

              int x=100;
           } 
        }


Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
        YouTube : Android & Java Tutorial

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Constructor in Java