In Java and Android, the order in which blocks of code, static code blocks are executed

Source: Internet
Author: User

In Java and Android, what is the difference between the execution order of blocks of code and static code blocks?

Java

Let's start with a simple example.

Main.java:

publicclass Main {    staticint1;    static {        System.out.println(a);    }    static {        2;    }    publicstaticvoidmain(String[] args) {        System.out.println("Hello World!");        System.out.println(a);    }}

Output to

1
Hello world!
2

After attempting to exchange static variable declarations and static code blocks, compile an error "illegal forward reference".
Interactive two code block position after output

2
Hello world!
2

Visible static decorated, from top to bottom, executed sequentially. Static variables and code blocks take precedence

Let's do a more complicated experiment.

 Public  class Main {    Static intA =1;Static{System.out.println (a); }Static{a =2; } Public Static void Main(string[] args) {System.out.println ("Hello world!");        Yo.test (); Yo yooo =NewYo ();    Yooo.test (); }} Public  class Yo {    StaticString name ="Mark";Static{System.out.println (name); } Public Yo() {Super(); System.out.println ("Constructor"); }Static  Public void Test() {System.out.println ("Hello"+ name); }}

Output

1
Hello world!
Mark
Hello Mark
Constructor
Hello Mark

Yo's execution conforms to the above conclusion, the static code block is executed, and after you construct the Yo instance, no static code blocks are executed repeatedly.

Android
 Public  class testapplication extends baseapplication {    Static{LOG.D ("Test","Application 1123"); }Static intA =1;Static{LOG.D ("Test","Application"+ a);    Homeactivity.test (); }Static{a =2; }@Override    protected void Attachbasecontext(Context Base) {LOG.D ("Test","Application Hello"+ a); }} Public  class splashactivity extends baseactivity {    Static{LOG.D ("Test","splashactivity Static code block"); }Static intA =1;Static{LOG.D ("Test","Splashactivity"+ a); }Static{a =2; }@Override    protected void onCreate(Bundle savedinstancestate) {LOG.D ("Test","Splashactivity onCreate"+ a);//Omit, will go to start homeactivity}} Public  class homeactivity extends baseactivity {     Public Static FinalString TAG ="Homeactivity";Static{LOG.D ("Test","Homeactivity 1123"); } Public Static void Test() {LOG.D ("Test","Homeactivity"); }}

The output result is

D/test:application 1123
D/test:application 1
D/test:homeactivity 1123
D/test:homeactivity
D/test:application Hello 2
D/test:splashactivity Static code block
D/test:splashactivity 1
D/test:splashactivity OnCreate 2

There's no difference between Java and the results.

The same is done first by static, static initialization of other classes outside the entrance, when the class is loaded by the JVM (from the result, it is loaded when the class is used).

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

In Java and Android, the order in which blocks of code, static code blocks are executed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.