The sequence of program initiation, and the understanding of instance variables assigned to each other and transferred copies

Source: Internet
Author: User

1  Public classInstanceinitializer {2      Private inti =1;3      Private intGeti () {4         returni;5     }6      Private intj =Geti ();7       8  9       PublicInstanceinitializer ()Ten       { Onei =2; A        } -      Public Static voidMain (string[] args) { -Instanceinitializer II =NewInstanceinitializer (); theSystem. out. println (II.J); -System. out. println (II.I); -     } -}

The results of the above program operation:

1  Public classInstanceinitializer {2      Private inti = 1;3      Private intj =Geti ();4      Private intGeti () {5         returni;6     }7      8       9  Ten       PublicInstanceinitializer () One       { Ai = 2; -        } -      Public Static voidMain (string[] args) { theInstanceinitializer II =NewInstanceinitializer (); - System.out.println (II.J); - System.out.println (ii.i); -     } +}

The results of the above program operation:

1  Public classInstanceinitializer {2       PublicInstanceinitializer ()3       {4i = 2;5        }6      Private inti = 1;7      Private intj =Geti ();8      Private intGeti () {9         returni;Ten     } One       A      Public Static voidMain (string[] args) { -Instanceinitializer II =NewInstanceinitializer (); - System.out.println (II.J); the System.out.println (ii.i); -     } -}

The results of the above program operation:

1  Public classInstanceinitializer {2       Private intJ=Geti ();3       private int i = 1; 4        PublicInstanceinitializer ()5       {6i = 2;7        }8       Private intGeti () {9          return  i; Ten        } One   A      Public Static voidMain (string[] args) { -Instanceinitializer II =NewInstanceinitializer (); - System.out.println (II.J); the System.out.println (ii.i); -     } -}

The results of the above program operation:

The above procedure, because when the instance variable j is defined, the dispatched function needs a copy of the other instance variable I, but at this time the instance variable is not defined and initialized, so the function failed to be dispatched at this time.

The comparison results show that: 1. Instance variable definition, 2. initialization, 3. A function definition called for initialization of an instance variable contains an instance variable 4 that has been successfully defined and initialized. The definition of a constructor is

The basic order in which programs are edited and run.

That is, the program is initialized in the order of the instance variables in your writing program. (class variables are first defined and initialized)

The reciprocal assignment of an instance variable is a copy, not a reference.

The sequence of program initiation, and the understanding of instance variables assigned to each other and transferred copies

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.