Tuesday, December 29, 2015

Static: what are the static variable and the static method?

We have a question here: what are the static variable and the static method? We will see what the static variable is and what the static method is in this post.

Definition


static variables: variables allocated for a class. Data stored in static variables is common for all the instances of that class.
static methods: methods allocated for a class. Static methods are common for all the instances of that class, so you don't need to make an instance to use that static method.

non-static variables:  variables allocated for an instance. Data stored in non-static variables is NOT common for all the instances of that class. The data stored changes its value depending instances even if they are made from a same class.

Static variables


At first, we will see an ordinal code without using static:

public class Test { public static void main(String args[]) { SubTest subtestone = new SubTest(); subtestone.a = 1; subtestone.b = 2; SubTest subtesttwo = new SubTest(); subtesttwo.a = 3; subtesttwo.b = 4; System.out.println(subtestone.a); System.out.println(subtestone.b); System.out.println(subtesttwo.a); System.out.println(subtesttwo.b); } } class SubTest{ int a; // these variables are not static int b; }
the result

The integer-variables "a" and "b" change its values by being assigned values. This is because the variables are allocated for the object, not for the class, thus, the each variable can have a different value every time we make a new object.

Now we will see how static variables work:

public class Test { public static void main(String args[]) { SubTest subtestone = new SubTest(); subtestone.a = 1; subtestone.b = 2; SubTest subtesttwo = new SubTest(); subtesttwo.a = 3; subtesttwo.b = 4; System.out.println(subtestone.a); System.out.println(subtestone.b); System.out.println(subtesttwo.a); System.out.println(subtesttwo.b); } } class SubTest{ static int a; //These variables are static static int b; }

the result

This time, all of the integer-variables "a" and "b" are treated as "3" and "4". This is because the variables are allocated for the class, not for the object, thus, the each variable can NOT have a different value every time we make a new object. Data stored in static variables is common for all the objects( or instances ) of that class. See the result above.

But, needless to say, if these static variables are allocated for the class which has the main method, the variables "a" and "b" change its values as follows:
public class Test { static int a; //These variables are static. static int b; public static void main(String args[]) { a = 1; b = 2; System.out.println(a); System.out.println(b); a = 3; b = 4; System.out.println(a); System.out.println(b); } }

the result


Static variables are allocated for the class, not for the object (or the instance), so even though they are static variables, they change its values depending on what value we assign to the variables.

Static methods

Static methods have same concept as static variables. Static methods are common for all the objects( or instances ) of that class.
At first, we will see an ordinal code without using static:

public class Test { public static void main(String args[]) { SubTest subtestone = new SubTest(); subtestone.showValue("My name is John."); SubTest subtesttwo = new SubTest(); subtesttwo.showValue("My name is Alice."); } } class SubTest{ void showValue(String word){ // This method is not static. System.out.println(word); } }

the result

The method is not static one, so the method is allocated for the object which we've created.

Now we will see how static methods work:
public class Test { public static void main(String args[]) { SubTest.showValue("My name is John."); SubTest.showValue("My name is Alice."); } } class SubTest{ static void showValue(String word){ // This method is static. System.out.println(word); } }

the result is same as the previous one.

The method is static one, so the method is allocated for the SubTest. Therefore, to use the static method, we don't need to make a new object. We can directly access to the static method of SubTest class.