Java Programming Idea (4th edition) initialization of static data

Source: Internet
Author: User

No matter how many objects are created, static data always consumes only one copy of storage space . The static keyword cannot be applied to local variables, so it can only be used for fields. If a domain is a static base type field and does not initialize it, it will get a standard initial value for the base type, and if it is a reference to an object, the default initializer is null. Examples are as follows:

Package Test;class bowl{bowl (int marker) {System.out.println ("Bowl (" +marker+ ")");} void F1 (int marker) {System.out.println ("F1 (" +marker+ ")");}} Class Table{static Bowl bowl1=new Bowl (1); Table () {System.out.println ("table ()"); Bowl2.f1 (1);} void F2 (int marker) {System.out.println ("F2 (" +marker+ ")");} Static Bowl bowl2=new Bowl (2);} Class Cupboard{bowl Bowl3=new Bowl (3); static Bowl bowl4=new Bowl (4); Cupboard () {System.out.println ("cupboard ()"); Bowl4.f1 (2);} void F3 (int marker) {System.out.println ("F3 (" +marker+ ")");} Static Bowl bowl5=new Bowl (5);} public class Static {public static void main (String args[]) {System.out.println ("Creating New Cupboard () in main"); new CUPB Oard (); System.out.println ("Creating New Cupboard () in main"); new cupboard (); Table.f2 (1); cupboard.f3 (1);} Static table Table=new table (), Static cupboard cupboard=new cupboard ();

The introduction of the bowl class allows you to see the creation of classes. The table class and the cupboard class include static data members of type Bowl in their class definitions. Note that before a static data member is defined, the cupboard class first defines a non-static data member Bowl3 of the bowl type.

Output Result:


Visible from the output, static initialization occurs only at the necessary time,If you do not create a Table object and do not reference TABLE.BOWL1 or TABLE.BOWL2, then static bowl BOWL1 and BOWL2 will never be created. only the first table object is created (or the first time the static data is accessed), they before it is initialized .thereafter, Static objects will no longer be initialized

From the output, it is known that the order of initialization is a static object (provided that they have not been initialized because of the previous object creation) and then non-static objects.


Java Programming Idea (4th edition) initialization of static data

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.