A summary of the problem of initialization of variables in Java

Source: Internet
Author: User
Tags first row
One, member variable initialization way
Prerequisite: Java guarantees that all member variables have a default initial value (0,false,null), which is called automatic initialization

1. Assign the value in the Declaration;

    * Example: private int i = 1;
        private String  name = "";
        Private Object = new Object ();

    * You can also call methods in the class to provide the initial value
    * method can also take the parameter public
        class Initbymethod
        {
            private int i = f ();
            private int J = g (2);
            public int f ()
            {return
                1;
            }
            public int g (int n)
            {return
                n;
            }
        }           
2, set the value in the constructor;
    。。。
3, initialization block;
    * Example: public class Employee
        {
            private int id;
            private name name;
            Initialize block
            {
                id=0;
                Name=new name ()
            }
        class name
        {
            private String firstName;
            Private String lastName;
            Name () {
                System.out.print ("First-last-name");
            }
    * Initialization block Execution time:
        as soon as the object of the class is constructed, the blocks are executed, and then the body of the constructor is run, and the
    number of times the execution occurs:
        multiple times, that is, whenever the constructor is called (regardless of which constructor), the initialization block is executed once;
4, static initialization; * * Assign values to static member variables when declared
            Example:
                    private static int i = 1;
                    Private static employee Em=new employee (); 
            Note that there is no static local variable, that is, the static keyword can not be used for local variables;
* * Static initialization block
            Example: public class Employee
                {
                    private static int id;
                    private static name name;
                    Static initialization block
                    static {
                        id=0;
                        Name=new name ()
                    }
                class name
                {
                    private String firstName;
                    Private String lastName;
                    Name () {
                        System.out.print ("First-last-name");
                    }
                
* Execution time for static initialization:
When an object of this class is first generated (that is, when the class first loads), or when the first time a static data member belonging to that class is accessed
* Execution number of static initialization:
Execute only once (class is first loaded)
second, the initialization of local variables
For the method's bureau invariants, Java implements this assurance in the form of a compile-time error
the sequence of initialization data;
 when a new object is created (a new object); new Dog (); 1, all data fields are automatically initialized to the default (0,false,null) (Allocate storage space for this object on the heap, which will be cleared 0) 2, Load the Dog.class class, performing static initialization in the order of declaration (static member variables are assigned in the Declaration). Static initialization block 3, in the order in which they appear in the declaration, performs non-static initialization (assignment of a non-static variable in the Declaration, initialization block) 4, if the second constructor is invoked in the first row of the constructor , the second constructor 5 is executed, and the constructor 
is executed.

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.