|
1 |
| -# java-static-members-lesson |
2 |
| -the importance of Java static members of a class cannot be overlooked |
| 1 | +# Java Static Members of a Class |
| 2 | +At first, I was just ignoring the importance of a variable or a method being static. |
| 3 | +And my understanding at first was quite obscure but then I realized how important |
| 4 | +they are in computer programming. |
| 5 | + |
| 6 | +A static member, whether a method or a variable, can exist on its own the moment |
| 7 | +you run a program. So, you can access them prior to any object creation. That is |
| 8 | +the very reason why the main method is static. It should exist prior to any |
| 9 | +object because it is the entry point of any running program in Java. |
| 10 | + |
| 11 | +Another important lesson here: once you created an object from a class that contains |
| 12 | +static variable, that object will not create a copy of its own static variable, |
| 13 | +it will always refer to the original static variable. This is very powerful in |
| 14 | +programming. For instance, you want to keep track of a certain variable whether |
| 15 | +its value is changing that will be used by several objects that depend on its status. |
| 16 | +It is, in essence, a global variable. |
| 17 | + |
| 18 | +`SampleStatic.java` |
| 19 | + |
| 20 | +``` |
| 21 | +class SampleStatic { |
| 22 | + //a static block |
| 23 | + //this is needed to initialize or load things |
| 24 | + //take note, this static block is loaded only ONCE |
| 25 | + //the moment you run the code, |
| 26 | + //for demonstration purposes, we just display a |
| 27 | + //default message here, |
| 28 | + static { |
| 29 | + System.out.println("A static member is called, " + |
| 30 | + "this is a default message, " + |
| 31 | + "independent of any object."); |
| 32 | + } |
| 33 | + |
| 34 | + //a static variable, |
| 35 | + //it is considered as global variable |
| 36 | + //when this is changed in an object, |
| 37 | + //the value will be reflected on all |
| 38 | + //objects created from this class |
| 39 | + static Integer staticVar = 5; |
| 40 | + |
| 41 | + //a static method, independent of any object |
| 42 | + //and can be called before any object exists |
| 43 | + //just like the main method |
| 44 | + static void staticMeth() { |
| 45 | + System.out.println("static `staticVar` is " + |
| 46 | + staticVar); |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +And in main: `MainMethod.java` |
| 52 | + |
| 53 | +``` |
| 54 | +class MainMethod { |
| 55 | + public static void main(String[] args) { |
| 56 | + //a call to the static method of `SampleStatic` class |
| 57 | + SampleStatic.staticMeth(); |
| 58 | + |
| 59 | + //create an instance of `SampleStatic` |
| 60 | + //to demonstrate |
| 61 | + //the behavior of static members |
| 62 | + SampleStatic sample1 = new SampleStatic(); |
| 63 | + //this will change the original |
| 64 | + //value of the static variable |
| 65 | + //`staticVar` and will be shared |
| 66 | + //by all objects |
| 67 | + System.out.println("`staticVar` is changed by sample1 object so: "); |
| 68 | + sample1.staticVar = 10; |
| 69 | + SampleStatic.staticMeth(); |
| 70 | + |
| 71 | + //another object that will share |
| 72 | + //the same static variable |
| 73 | + SampleStatic sample2 = new SampleStatic(); |
| 74 | + System.out.println("`sample2` object shares " + |
| 75 | + "the same variable: `staticVar` = " + |
| 76 | + sample2.staticVar); |
| 77 | + |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +the result will be: |
| 83 | + |
| 84 | +``` |
| 85 | +> run MainMethod |
| 86 | +A static member is called, this is a default message, independent of any object. |
| 87 | +static `staticVar` is 5 |
| 88 | +`staticVar` is changed by sample1 object so: |
| 89 | +static `staticVar` is 10 |
| 90 | +`sample2` object shares the same variable: `staticVar` = 10 |
| 91 | +``` |
| 92 | + |
| 93 | +## static block |
| 94 | +As demonstrated above, if you need to initialize things, do it in |
| 95 | +the static block. The moment you run the code, it is loaded ONCE. |
| 96 | + |
| 97 | +## static method |
| 98 | +The static method has restrictions compared to a regular method. |
| 99 | +> It can only call directly other static methods. |
| 100 | +
|
| 101 | +> It can only call directly static variables. |
| 102 | +
|
| 103 | +> It cannot refer to *this* and *super* because it does not depend |
| 104 | +on any object creation. |
| 105 | + |
| 106 | +## static variable |
| 107 | +As was mentioned, it is essentially a global variable. If ever it is changed |
| 108 | +directly or maybe through an object, all objects and direct calls to it |
| 109 | +manifest that change. |
0 commit comments