The order in which Java objects are initialized

Source: Internet
Author: User

Java code

  1. Public class Son extends Father {

  2. String value = null; //2

  3. Public Son () {

  4. Super  (); //1

  5. System.out.println ("Son:" + value); //3

  6. }

  7. Public Static void Main (final string[] args) {

  8. New Son ();

  9. }

  10. }

  11. class Father {

  12. Public Father () {

  13. if ( This instanceof Son) {

  14. Son lower = (son) this;

  15. Lower.value = "Test";

  16. }

  17. }

  18. }

  19. class Father {

  20. Public Father () {

  21. if ( This instanceof Son) {

  22. Son lower = (son) this;

  23. Lower.value = "Test";

  24. }

  25. }

  26. }


Download

The result of this is null
Step 1 set to test
Step 2 is set to NULL
Step 3 Print out null

If it is not String value = null; Just String value; Download

Step 1 set to test
Step 2 do nothing, because there is already a value, do not set the default null value
Step 3 print out null
 

so a field that does not set a value and is set to NULL is different.  


The order in which Java objects are initialized

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.