Java-4.7 constructor initialization (2)

Source: Internet
Author: User

Java-4.7 constructor initialization (2)

This chapter is followed by the content of the previous chapter.

(3) static data Initialization

A. static data is only executed once.

package com.ray.testobject;public class Test {private static Book book1 = new Book(1);private static Book book2 = new Book(2);public Test() {System.out.println(create test);book1.say();book2.say();Book book3 = new Book(3);book3.say();}public static void main(String[] args) {new Test();new Test();}}class Book {private int id;public Book(int id) {this.id = id;System.out.println(create book: + id);}public void say() {System.out.println(hi, my name is book- + id);}}


Output:

Create book: 1
Create book: 2
Create test
Hi, my name is book-1
Hi, my name is book-2
Create book: 3
Hi, my name is book-3
Create test
Hi, my name is book-1
Hi, my name is book-2
Create book: 3
Hi, my name is book-3


As you can see from the results, book1 and book2 are created only once when the Test is created for the first time, and then no more book1 and book2 are created when the Test is created again.

 

B. If it is not referenced or new, the static attribute fields in the object will not be created.

Let's modify the above Code:

package com.ray.testobject;public class Test {public Test() {}public static void main(String[] args) {}}class Book {private int id;public Book(int id) {this.id = id;System.out.println(create book: + id);}public void say() {System.out.println(hi, my name is book- + id);}}class GroupOfBooks {private static Book book;public static Book getBook() {book = new Book(99);return book;}}

 

The above code will not execute the new Book (99) line.

Let's modify it again and give two examples of executing new Book (99:

package com.ray.testobject;public class Test {public Test() {}public static void main(String[] args) {new GroupOfBooks();}}class Book {private int id;public Book(int id) {this.id = id;System.out.println(create book: + id);}public void say() {System.out.println(hi, my name is book- + id);}}class GroupOfBooks {private static Book book;public static Book getBook() {book = new Book(99);return book;}}

 

package com.ray.testobject;public class Test {public Test() {}public static void main(String[] args) {GroupOfBooks.getBook();}}class Book {private int id;public Book(int id) {this.id = id;System.out.println(create book: + id);}public void say() {System.out.println(hi, my name is book- + id);}}class GroupOfBooks {private static Book book;public static Book getBook() {book = new Book(99);return book;}}


The above two pieces of code are also output:

Create book: 99

 

Here we will summarize the object creation process:

1. query the path of the. class File

2. Load the. class file and execute the static method or static attribute field of the object.

3. new object, and then allocate memory space (if the memory space is cleared, the object will be recycled and the reference will become null)

4. Execute attribute domain Initialization

5. Execute the constructor

 

Summary: This chapter mainly discusses static attribute domain initialization.

 

This chapter is here. Thank you.

 

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.