Comprehensive use of Singleton model and static loading (Youku interview questions), static model

Source: Internet
Author: User

Comprehensive use of Singleton model and static loading (Youku interview questions), static model
I. original question

1/* 2 * running result 3*2 4*1 5 */6 public class BookTest {7 static BookTest book = new BookTest (); 8 static int; 9 static int B = 0; 10 11 private BookTest () {12 a ++; 13 B ++; 14} 15 16 public static void main (String [] args) {17 BookTest book = new BookTest (); 18 System. out. println (book. a); 19 System. out. println (book. b); 20} 21 22}

 

Ii. original question explanation
1 package com. heima. when the test; 2 3/* 4 * program is executed, jvm first loads the Book class, then new Book (), and then static variables a and B; but 5 * at this time, no value is assigned to a and B (using the default value corresponding to its data type, that is, two static variables a = 0, B = 0 6 * loaded into memory), so the new Book () calls its private constructor to output a = 1, B = 1; and then assigns the initial value to the static variable, 7 * That is, the value a given by US remains unchanged, and B changes to 0 (now a = 1, B = 0). Then, in the main method, a new Book () object is created, call the constructor eight times, that is, a is changed to 2, B is changed to 1. 9 */10 11/* 12 * final execution result: 13 the constructor is called. Before calling: 14 a = 0 B = 015 after the constructor is called: 16 a = 1 B = 117 static code block called: 18 a = 5; B = 019 constructor called. Before calling: 20 a = 5 B = 021 after calling the constructor: 22 a = 6 B = 123 624 125 */26 public class Book {27 static Book book = new Book (); 28 static int a = 5; 29 static int B = 0; 30 static {31 System. out. println ("static code block called: \ na =" + a + "; \ tb =" + B); // you can also use Book. a and Book. b32} 33 34 private Book () {35 System. out. println ("constructor called, before calling:"); 36 System. out. print ("a =" + a + "\ tb =" + B); // you can also use Book. a and Book. b37 a ++; 38 B ++; 39 System. out. println ("\ n constructor called: \ na =" + a + "\ tb =" + B); 40} 41 42 public static void main (String [] args) {43 Book book = new Book (); 44 System. out. println ("a last equals:" + book. a); 45 System. out. println ("B is equal to:" + book. b); 46} 47 48}

 

Related Article

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.