• When we declare a variable inside a class directly without a static keyword, it is called an instance variable.
• Instance variable is also known as “Non-Static Variable”.
• An instance variable gets its memory at the time of class object creation.
class JTC{
public static void main(String arg[]){
Student s1 = new Student();
s1.sid = 101;
s1.sname = "Vivek";
s1.semail = "vivek@jtcindia.org";
Student s2 = new Student();
s2.sid = 102;
s2.sname = "Rahul";
s2.semail = "rahul@jtcindia.org";
System.out.println("----Student Details----");
System.out.println("s1.sid :- "+s1.sid);
System.out.println("s1.sname :- "+s1.sname);
System.out.println("s1.semail :- "+s1.semail);
System.out.println("----Student Details----");
System.out.println("s2.sid :- "+s2.sid);
System.out.println("s2.sname :- "+s2.sname);
System.out.println("s2.semail :- "+s2.semail);
}
}
class Student{
int sid;
String sname;
String semail;
}
----Student Details---- s1.sid :- 101 s1.sname :- Vivek s1.semail :- vivek@jtcindia.org ----Student Details---- s2.sid :- 102 s2.sname :- Rahul s2.semail :- rahul@jtcindia.org
In the above example we have a class student having 3 instance variables: sid, sname and semail. In main method we are creating two objects of student class: Student s1 = new Student() and Student s2 = new Student().As we discussed above that an Instance variable gets its memory at the time of its class object creation; hence sid, sname and semail get different memory allocations for two different objects of student class. In this example we can see how we are using instance variables to store a student’s unique data.
• When we declare a variable inside the class directly with a static keyword, it is called static variable.
• A static variable gets its memory at the time of its class loading.
• A static variable gets its memory allocation once in its life-Cycle.
• Static variable is basically used to store the data which is common to all users.
class JTC{
public static void main(String arg[]){
Student s1 = new Student();
s1.sid = 101;
s1.sname = "Vivek";
s1.semail = "vivek@jtcindia.org";
Student s2 = new Student();
s2.sid = 102;
s2.sname = "Rahul";
s2.semail = "rahul@jtcindia.org";
System.out.println("----Student Details----");
System.out.println("s1.sid :- "+s1.sid);
System.out.println("s1.sname :- "+s1.sname);
System.out.println("s1.semail :- "+s1.semail);
System.out.println("s1.snationality :- "+s1.snationality);
System.out.println("s2.sid :- "+s2.sid);
System.out.println("s2.sname :- "+s2.sname);
System.out.println("s2.semail :- "+s2.semail);
System.out.println("s2.snationality :- "+s2.snationality);
}
}
class Student{
int sid;
String sname;
String semail;
static String snationality = "Indian";
}
----Student Details---- s1.sid :- 101 s1.sname :- Vivek s1.semail :- vivek@jtcindia.org s1.snationality :- Indian s2.sid :- 102 s2.sname :- Rahul s2.semail :- rahul@jtcindia.org s2.snationality :- Indian
In the above program we have a student class having three instance variables: sid, sname and semail and one static variable snationality and its initial value is Indian. Since sid, sname and semail are instance type variables we get different memory allocations for different objects and since snationality is a static type of variable we can see in the image that same memory allocation is sharable with all the objects of student class.
In Java we have three different ways to access a static variable:
a. We can access a static variable along with its class name.
b. We can access a static variable using its class type reference variable which contains the null value.
c. A static variable can be accessed using its class object.
class JTC{
public static void main(String arg[]){
System.out.println("---- Accessing Static Variable Using their Class Name----");
System.out.println("A.a :- "+A.a);
System.out.println("---- Accessing Static Variable Using their Class reference variable which is containing null value----");
A a1 = null;
System.out.println("a1.a :- "+a1.a);
System.out.println("----Accessing Static Variable Using their Class Object----");
A a2 = new A();
System.out.println("a2.a :- "+a2.a);
}
}
class A{
static int a = 10;
}
---- Accessing Static Variable Using their Class Name---- A.a :- 10 ---- Accessing Static Variable Using their Class reference variable which is containing null value---- a1.a :- 10 ----Accessing Static Variable Using their Class Object---- a2.a :- 10
Here in the above program, we have a class which is A that contains static type of variable static int a = 10. As we discussed, we can access a static variable using its class name or class reference variable with null value or class object. Here we are accessing static type variable using all these approaches.