Java Tutorial


OOPS concept


Collection

Java - Variables

Variables are the name given memory locations.

There are three types of variables in java.

  • Local Variables
  • Instance Variables
  • Static Variables
  • Local Variables

    Local variables are the variables which are declared inside a method or blocks.

    Example

    class Student
    {
     int add(int a, int b)
     {
       int c=0; //Local Variable
       c=a+b;
       return c;
     } 
    }
    

    Instance Variables

    These are the variables which are declared inside the class and outside the method or block.

    Example

    class Student
    {
      int x;        //Instance variable
      String name;  //Instance variable
    
     int add(int a, int b)
     {
       ...
     } 
    }
    

    Static Variables

    Static variables are declared with static keyword. The static variables are intialized only once.

    Example

    class Student
    {
      int x;        
      String name;  //Instance variable
      static int count = 0; //Static variable
     int add(int a, int b)
     {
       ...
     } 
    }
    

    This topics will be more clear when we do practical questions.

    Advertisment

    Share


    Advertisement