Constructor initialization _02

Source: Internet
Author: User

Cannot prevent automatic initialization, it will occur before the constructor is called.

The order in which the variables are defined determines the order in which they are initialized, even if the definition of the variable is scattered between the method definitions, it will still be initialized before any method, including the constructor.

Import static net.mindview.util.print.*;//when the constructor are called to create a//Window object and you'll see a messag E:class window {  window (int marker) {print ("window (" + marker + ")");}} Class House {  window w1 = new Window (1),//before constructor house  () {    //Show that we ' re in the constructor :    print ("house ()");    W3 = new Window (33); Reinitialize W3  }  window w2 = new Window (2),//after constructor  void F () {print ("F ()");}  Window W3 = new window (3); At End}public class Orderofinitialization {public  static void Main (string[] args) {House    H = new House (); 
   H.F (); Shows that construction was done  }}//Output:window (1) window (2) window (3) House () window (+) F ()

The visible initialization operation is in the first place.

Static data initialization, see the following code

Import static Thinkinginjava. Print.*;class Go {    static string s1 = "Run";    static string s2, s3;    s tatic {                             //static block or static sentence Child (first executed, and only once, pay attention to the output) no.1        s2 = "Drive Car";        S3 = "Fly Plane";        print ("S2 & S3 Initialized");    }     static void How () {        print (S1 + "or" + s2 + "or" + S3);  &N Bsp  }    go () {print ("Go ()");}     }public class Explicitstaticex {    public static void Main (string[] args) { & nbsp       print ("Inside Main ()"); no.4        go.how ();//no.5        print ("GO.S1:" + GO.S1);//no.6    &NBsp;       }    static Go g1 = new Go (); no.2    static Go g2 = new Go ();//no.3}
Output

S2 & S3 Initialized
Go ()
Go ()
Inside Main ()
Run or drive car or fly plane
Go.s1:run

If the initialization operation outside the main method is not static, i.e. no static, then these two sentences will not be executed. After adding the static, they will be executed before the main method and will only be initialized once.

Constructors can be thought of as static methods.


Constructor initialization _02

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.