Static code block, construction code block, partial code block demonstration

Source: Internet
Author: User

 public class test {static int num; static int numobj; // records how many objects are generated! Static {// static code block, used to initialize the class! // Num = 10; ++ num; num * = 12; // before entering the static code block, the num initialization value is 0 system. out. println (Num); // main (null); // you can call main in a static code block. If you think about it, it is static!} // Execute the constructor code segment first, and then the row constructor {// This is called the constructor code segment. You can initialize all objects! Every object is executed when it is generated! ++ Numobj; system. Out. println ("construct a code segment! ");} Public test () {// count the object instance in the constructor // + numobj; system. Out. println (" constructor! ");} Public void finalize () {// call system. Out. println (" Object release! "); -- Numobj;} public static void show () {system. out. println ("number of instances created:" + numobj);} public static void main (string [] ARGs) {system. out. println ("the static code segment is used to initialize the class! "); Test T1 = new test (); t1.show (); {// partial code segment new test ();} system. GC (); // start the Garbage Collector! New Test (); new test (); system. Out. println (test. numobj) ;}

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.