A variable is the name given to a memory location. It is the basic unit of storage in a program. - The value stored in a variable can be changed during program execution. - A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. In Java, all the variables must be declared before they can be used. ## How to declare variables?  ## Types of variables - **Local Variables** : A variable defined within a block or method or constructor is called local variable. - The scope of these variables exists only within the block in which the variable is declared ``` public class StudentDetails { public void StudentAge() { //local variable age int age = 0; age = age + 5; System.out.println("Student age is : " + age); } public static void main(String args[]) { StudentDetails obj = new StudentDetails(); obj.StudentAge(); } } ``` ``` Student age is : 5 ``` ``` public class StudentDetails { public void StudentAge() { //local variable age int age = 0; age = age + 5; } public static void main(String args[]) { //using local variable age outside it's scope System.out.println("Student age is : " + age); } } ``` ``` error: cannot find symbol " + age); ``` - **Instance Variables** : Instance variables are non-static variables and are declared in a class outside any method, constructor or block. - As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed. - Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used. ``` import java.io.*; class Marks { //These variables are instance variables. //These variables are in a class and are not inside any function int engMarks; int mathsMarks; int phyMarks; } class MarksDemo { public static void main(String args[]) { //first object Marks obj1 = new Marks(); obj1.engMarks = 50; obj1.mathsMarks = 80; obj1.phyMarks = 90; //second object Marks obj2 = new Marks(); obj2.engMarks = 80; obj2.mathsMarks = 60; obj2.phyMarks = 85; //displaying marks for first object System.out.println("Marks for first object:"); System.out.println(obj1.engMarks); System.out.println(obj1.mathsMarks); System.out.println(obj1.phyMarks); //displaying marks for second object System.out.println("Marks for second object:"); System.out.println(obj2.engMarks); System.out.println(obj2.mathsMarks); System.out.println(obj2.phyMarks); } } ``` - **Static Variables** : (Class variables) - These variables are declared similarly as instance variables, the difference is that static variables are declared using the **static keyword** within a class outside any method constructor or block. - **Unlike instance variables, we can only have one copy of a static variable per class irrespective of how many objects we create.** - Static variables are created at start of program execution and destroyed automatically when execution ends. access the variable as: ``` class_name.variable_name; ``` ``` import java.io.*; class Emp { // static variable salary public static double salary; public static String name = "Harsh"; } public class EmpDemo { public static void main(String args[]) { //accessing static variable without object Emp.salary = 1000; System.out.println(Emp.name + "'s average salary:" + Emp.salary); } } ``` ## Instance variable Vs Static variable - Each object will have its own copy of instance variable whereas We can only have one copy of a static variable per class irrespective of how many objects we create. - Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of instance variable. In case of static, changes will be reflected in other objects as static variables are common to all object of a class. - We can access instance variables through object references and Static Variables can be accessed directly using class name. - Syntax for static and instance variables: ``` class Example { static int a; //static variable int b; //instance variable } ```