Doesn't the execution of a Java program start with the main() method? Why is the first output of the following program not "in main" but rather "super static block"?
-
class StaticSuper { static { System.out.println(âsuper static blockâ); } StaticSuper() { System.out.println(âsuper constructorâ); } } public class StaticTests extends StaticSuper { static int rand; static { rand = (int) (Math.random() * 6); System.out.println(âstatic block â + rand); } StaticTests() { System.out.println(âconstructorâ); } public static void main(String [] args) { System.out.println(âin mainâ); StaticTests st = new StaticTests(); } } The output is: super static block static block 3 in main super constructor constructor Where does execution start? If it is from the main() method, why isn't in main the first output? Am assuming it gets printed before StaticTests constructor gets invoked. This constructor in turn calls the constructor in StaticSuper. At this point, the static initializer in StatiSuper get executed then the object is created. Execution then goes back to the constructor in StaticTests, etc... I know that a static initializer runs before a constructor. But if the whole program execution is starting from the main() method, shouldn't "in main" be printed first?
-
Answer:
Class static code is executed when the class is loaded by the class loader. The class has to be loaded before its main can even be found. It's confusing, that's why class static code is to be avoided.
Jason Barrett Prado at Quora Visit the source
Other answers
Expanding on Jason's answer, maybe it will help to dig a bit deeper. You did something like this -> > java StaticTests The JVM tries to call the main method, but realizes the class is not loaded, so it asks a classloader to look at the classpath and find a binary for that class. The class is now loaded, for all the JVM knows this might be a poorly formed or corrupted class, so it does a bunch of verification steps, if all is well it prepares some internal storage and then tries to see if it can load/resolve some referred classes. Then comes initialization Initialization consists of execution of class variables initializers and static initializers of the class, this also involves initializing all the super-classes recursively . Finally after all this is done , The method StaticTests.main is called. You can actually see this in bytecode [code] public class StaticTests extends StaticSuper { // compiled from: StaticTests.java // access flags 0x8 static I rand // access flags 0x0 <init>()V //constructor block // access flags 0x9 public static main([Ljava/lang/String;)V //the main part // access flags 0x8 //Class init routines static <clinit>()V L0 LINENUMBER 17 L0 INVOKESTATIC java/lang/Math.random ()D LDC 6.0 DMUL D2I //..... } [/code] Observe that all code from your static block is grouped into <clinit>, try putting a few more static blocks, You will see that they all get bunched up in <clinit> ,this method is called when class initialization happens, and its recursively called for the inheritance chain. Hope this answers your question
Ashwin Rajeev
Related Q & A:
- Why doesn't it work when I want to click a link in my Yahoo mess list?Best solution by Yahoo! Answers
- Why doesn't a Nissan truck run smooth in overdrive?Best solution by Yahoo! Answers
- Why doesn't a computer recognise a printer?Best solution by Yahoo! Answers
- Why doesn't Youtube work on a laptop?Best solution by Yahoo! Answers
- Why doesn't Firefox start up?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.