public class MaClass{ { System.out.print("SCEA, "); } static{ System.out.print("SCJP, "); } static String s1=getVal(); static String s2="Web Componet Developer, "; private static String getVal(){ System.out.print("Mobile Application Developer, "); return s2; } public static void main(String args[]){ System.out.print(s1); } }
Output: SCJP, Mobile Application Developer, NULL
Now I understand that 1st static block is executed, which prints SCJP then getVal() prints Mobile Application Developer, and returns S2 now how S2 is coming as null, because Its static variable so it should be initialized when class is loaded so I think that when main method is called of that class S2 should be intialized to its degfault value "Web Componet Developer, " but why is it giving NULL ?
Hi Vikram,
Loading a class to the memory, and initializing its static variables are two different things, even though that initialization happens right after the class is loaded to the memory.
Now, as soon as we use the class MaClass, it gets loaded to the memory with its variables and methods. However, these variables are not initialized in the phase of loading the class. Instead, initialization happens right after the class is being loaded. Until that happens, static variables contain their default values. This is a very short period though, but the program spans in that short period. You are also correct that when the main method gets invoked, the class should be fully loaded with its variables. But the weird thing happens BEFORE the main method gets called.
Let's see how:
Step 1: In this program, the first thing the JVM does is loading the Node class, because it needs to be loaded in order to run the main method. As it loads, all static variables will have their default values.
Step 2: Right after the class is loaded, it immediately starts to initialize the static variables with the given initializes. This happens in the casual top-to-bottom basis (as how a normal sequence of statements run).
Step 2.1 : The first statement it finds is the the static initialization block. So it prints "SCJP", which should be obvious.
Step 2.2 : Then it reaches the line #8 in the program (which is static String s1=getVal();). In this part, we says to initialize the variable s1 with the return value of getVal(). So it has to call the getVal() method.
Step 2.3 : getVal() prints "Mobile Application Developer", and returns the current value of s2. This is where the weird thing happens. Remember, we have only initialized up to line #8 by now. Variable s2 is something that is scheduled to initialize when we will reach the line #9. So at this point, s2 contains the default value, which is null as assigned to it when the class was loaded in our Step 1.
Step 2.4 : So the return value of getVal() turns out to be 'null', and that will be the value s1 gets initialized to.
Step 3: Now we are done with line #8 , so we can continue to initialize the remaining lines, starting from the line #9. Of course, line #9 will initialize s2 to "Mobile Application Developer", but that doesn't change the value we have already assigned to s1.
Step 4: After both loading and initialization parts done, we can now call the main method. At this point, s1 contains 'null' , and s2 contains "Mobile Application Developer".
Do you see the beauty of this program?
The output looks very linear: SCJP, Mobile Application Developer, NULL
However, the part in the red was actually printed even before the main method was started!. Main method only printed the part in blue!
To understand this further, try doing various tricks with the code and see the result it produces. For instance, try to swap the positions of line-8 and line-9 and see if that makes a difference.
Does that help?