Object initialization in Java (initialization of objects in Java)

Source: Internet
Author: User

Say no more, look directly at the code.

 Public classMyTest { Public Static voidMain (string[] args) {Test T1=NewTest (); System.out.println ("------------"); Test T2=NewTest (11); System.out.println ("------------"); }    }classtest{Private Static intb =Initializeb (); Private Static intA =Initializea (); Private intc =Initializec (); Private Static intInitializea () {System.out.println ("Initialize A In private static method"); return1; }        Private Static intInitializeb () {System.out.println ("Initialize B in private static method"); return1; }        protected Final intInitializec () {System.out.println ("Initialize C in private method for each object"); return1; }        Static //called once at class load time{System.out.println ("C # Static Constructor equivalent\n====static initialization is done===="); }    //called every time before the constructor//This is a shared among all the constructors.{System.out.println ("Non-static shared Block"); }             PublicTest () {System.out.println ("In the constructor without parameter"); }         PublicTest (intuseless) {System.out.println ("In the constructor with only 1 parameter"); }}

Console:

Initialize b in private static method
Initialize A in private static method
C # Static Constructor equivalent
====static initialization is done====
Initialize C in private method for each object
Non-static Shared block
In the constructor without parameter
------------
Initialize C in private method for each object
Non-static Shared block
In the constructor with only 1 parameter
------------

Double Brace Initialization:

See: http://www.c2.com/cgi/wiki?DoubleBraceInitialization

The first brace creates a new anonymousinnerclass, the second declares an instance initializer block which is run when the Anonymous inner class is instantiated. This type of initializer block was formally called an "instance initializer", because it was declared within the instance SC Ope of the class--"static initializers" is a related concept where the keyword static was placed before the brace that s Tarts The block, and which is executed at the class level as soon as the ClassLoader completes loading the class (Specifie D at http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.6) the initializer block can use any methods, Fields and final variables available in the containing scope, but one have to is wary of the fact that initializers is run Before constructors (but not before superclass constructors)

Object initialization in Java (initialization of objects in Java)

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.