Monday, 12 September 2016

JAVA BLOG_3

                          VARIABLES

          Variable is name of reserved area allocated in memory which memory is called as RAM . 

        Ex:: int data=1;//data is variable.

 

  • Types of Variables

    1)local variable

    2)Instance variable

    3)static variable

     

    Local Variable -:

                   A variable that is declared inside the method called local variable.

     

    Instance Variable -:

                       A variable that is declared inside the class but outside the method is called instance variable.It is not declared as static.

     

    Static Variable -:

                         A variable that is declared as static is called static variables.It cannot be local

     

     -------------------------------------------------------

    Program:

             class VariableDemo
             {
                    int a=1;
                    /*
                   (int a) is a not-static data member so it
                   gives Exception while excecuting the
                   Program.

                 so main method is a static method so 
                 only static data members can be 
                 initialized in main method.

                 if we want to execute instance non 
                 static data member then you have to 
                 create object for that not static member
                 then it will excecute successfully  
                  */

                 static int b=2;

                 public static void main(String args[])
                {
               VariableDemo n = new VariableDemo();
                     int c=3;
                     System.out.println("a:"+n.a);
                     System.out.println("b:"+b);
                     System.out.println("c:"+c);
                 }

         }

             

 

 

No comments:

Post a Comment