Types of Variables: =================== ==> three types of variables: 1) Local Variables 2) Instance Variables 3) Static Variables 1) Local variables: =================== ==> the local variable also called as "method variable". ==> we can access within the same method but we can't extend the local variable to the outside of the method. package AIT.IO.Java; class ClassA{ public void m1() { int x = 123; System.out.println(x); } public void m2() { // System.out.println(x); } } public class LocalVariables { public static void main(String[] args) { ClassA aobj = new ClassA(); aobj.m1(); } } =========================================== Differences between Instance Variables and Static variables: ============================================================ 1) The Value of the instance variable is always based on the object. package AIT.IO.Java; class Class1{ int x; // instance variable static int y; // static variable public void m1() { System.out.println("x = "+x); System.out.println("y = "+y); } } public class Variables { public static void main(String[] args) { Class1 c1 = new Class1(); Class1 c2 = new Class1(); c1.x = 123; c2.x = 112; c1.y = 121; System.out.println("The C1 Object data = "); c1.m1(); System.out.println("The c2 Object data = "); c2.m1(); } } ================================== 2) Modification: ================ ==> The modification of instance variable cannot reflect on other object. But the modification of static variable can reflect on other object of the class. package AIT.IO.Java; class Class1{ int x; // instance variable static int y; // static variable public void modification() { x = x + 100; y = y + 200; } public void m1() { System.out.println("x = "+x); System.out.println("y = "+y); } } public class Variables { public static void main(String[] args) { Class1 c1 = new Class1(); Class1 c2 = new Class1(); c1.x = 123; c2.x = 112; c1.y = 121; System.out.println("The C1 Object data = "); c1.m1(); System.out.println("The c2 Object data = "); c2.m1(); System.out.println("The C1 object data after the modification = "); c1.modification(); c1.m1(); System.out.println("The C2 object data after the modification = "); // c2.modification(); c2.m1(); } } =================================================== 3) When the memory can be created for instance variable and for static variable? ================================================================================= class ClassA{ int x; static int y; } public class MainClass{ public static void main(String[] args) { ClassA c1 = new ClassA(); ClassA c2 = new ClassA(); } } ==> The memory for the Instance Variable can be created at the time of object creation. whereas the memory for the static variable can be created at the time of loading the class into heap memory. package AIT.IO.Java; class Class1{ int x; // instance variable static int y; // static variable public void m1() { System.out.println("Instance Variable = "+x); System.out.println("Static Variable = "+y); } } public class Variables { public static void main(String[] args) { // Class1 c1 = new Class1(); // Class1 c2 = new Class1(); // c1.m1(); System.out.println(Class1.y); } } ============================================== 4) Do we access the instance variable with class name? ======================================================= Static variable can allow to access either with object or class name. whereas the instance variable can always access through the object only. package AIT.IO.Java; class Class1{ int x; // instance variable static int y; // static variable public void m1() { System.out.println("Instance Variable = "+x); System.out.println("Static Variable = "+y); } } public class Variables { public static void main(String[] args) { Class1 c1 = new Class1(); // Class1 c2 = new Class1(); // c1.m1(); System.out.println(Class1.y); // System.out.println(Class1.x); System.out.println(c1.y); System.out.println(c1.x); } } =============================================== 5) The Instance variable can always define within the non-static method only. Whereas the static variable can define in either static method or non-static method. package AIT.IO.Java; class Class1{ int x; // instance variable static int y; // static variable static void m1() { // System.out.println("Value of x = "+x); System.out.println("Value of y = "+y); } public void m2() { System.out.println("X = "+x); System.out.println("Y = "+y); } } public class Variables { public static void main(String[] args) { Class1 c1 = new Class1(); // c1.m1(); Class1.m1(); c1.m2(); } } =================================================