Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java object initialization     - JavaScript Advanced Programming notes event capture and event bubbling (Programming)

- Linux Getting Started tutorial: Experience QEMU virtual machine articles (Linux)

- Ubuntu dual-card system configuration method (Server)

- Through the source code to install MySQL 5.6.26 under CentOS6 (Database)

- How to query the role of Linux services (Linux)

- ASP.NET 5 tutorial series (Server)

- Java in several ways of using MongoDB (Programming)

- ORA-00600 error solve one case (Database)

- Red Hat Enterprise Linux Server 6.5 installation GCC 4.9.2 (Linux)

- Linux Getting Started tutorial: Ubuntu 14.04 in the installation Sogou Pinyin (Linux)

- SecureCRT use the configuration detailed tutorial (Linux)

- Use Spring cache and ehcache (Programming)

- Linux iptables: combat scenes (Linux)

- Improve the efficiency of Linux development tools 5 (Linux)

- Ubuntu installed Komodo editor by PPA (Linux)

- Upgrade to Linux Mint 16 petra Mint 17 Qiana (Linux)

- Linux Security (Linux)

- Ubuntu 15.10 15.04 14.10 14.04 Install Ubuntu Tweak (Linux)

- How to limit network bandwidth usage in Linux (Linux)

- Linux Basic Course: tar command description (Linux)

 
         
  Java object initialization
     
  Add Date : 2016-10-14      
         
         
         
  Automatic initialization (default)

All basic data members of a class are initialized, run the following example, you can view these default values:

class Default {
    boolean t;
    char c;
    byte b;
    short s;
    int i;
    long l;
    float f;
    double d;
    public void show () {
        System.out.println ( "basic type initialization value \ n" +
                        "Boolean <----->" + t + "\ n" +
                        "Char <----->" + c + "\ n" +
                        "Byte <----->" + b + "\ n" +
                        "Short <----->" + s + "\ n" +
                        "Int <----->" + i + "\ n" +
                        "Long <----->" + l + "\ n" +
                        "Float <----->" + f + "\ n" +
                        "Double <----->" + d + "\ n"
        );

    }
}
public class InitValue {
    public static void main (String [] args) {
        Default d = new Default ();
        d.show ();
    }
}

[Run] Results:
Basic types of initialization value
boolean <-----> false
char <----->
byte <-----> 0
short <-----> 0
int <-----> 0
long <-----> 0
float <-----> 0.0
double <-----> 0.0


Wherein, char type of default is an empty (null).

For non-basic data types, the handle of the object will be initialized:

class Person {
    private String name;
    // Setter
}
class Default {
    Person p;
    public void show () {
        System.out.println ( "Person <----->" + p);
    }
}
public class InitValue {
    public static void main (String [] args) {
        Default d = new Default ();
        d.show ();
    }
}

[Run] Results:
Person <-----> null


Visible, handle initialization value is null. That is, if you do not specify the value of p is called initialization method is similar p.setName, there will be an exception.

Provisions initialization

If you need to own a variable assigned an initial value can be assigned in the definition of variables at the same time.

class Default {
    boolean t = true;
    char c = 'A';
    byte b = 47;
    short s = 0xff;
    int i = 24;
    long l = 999;
    float f = 1.2f;
    double d = 1.732;
    public void show () {
        System.out.println (
                        "Boolean <----->" + t + "\ n" +
                        "Char <----->" + c + "\ n" +
                        "Byte <----->" + b + "\ n" +
                        "Short <----->" + s + "\ n" +
                        "Int <----->" + i + "\ n" +
                        "Long <----->" + l + "\ n" +
                        "Float <----->" + f + "\ n" +
                        "Double <----->" + d + "\ n"
        );

    }
}
public class InitValue {
    public static void main (String [] args) {
        Default d = new Default ();
        d.show ();
    }
}

It can even be initialized by a method;

class Person {
    int i = set ();
    // ...
}

These methods can also use the argument:

class Person {
    int i;
    int j = set (i);
    // ...
}

Initialize builder

Advantage of the builder is initialized initialization value can be determined at runtime. E.g:

class Person {
    int age;
    Person () {
        age = 89;
    }
}

age is first initialized to 0, and then a 89. For all handles basic types and objects, such cases are established.

The initialization sequence

In one class, the initialization sequence is defined by the variable order within the class of decision. Even if a large number of variable definitions are found in the middle of the method definition, the variable would still be initialized before calling any of the methods (including constructors). E.g:

class Pet {
    Pet (int age) {
        System.out.println ( "Pet (" + age + ")");
    }
}

class Person {
    Pet t1 = new Pet (1);

    Person () {
        System.out.println ( "--- Person () ---");
        t3 = new Pet (33);
    }

    Pet t2 = new Pet (2);
    void show () {
        System.out.println ( "show ---- running");
    }
    Pet t3 = new Pet (3);
}


public class OrderOfInitialization {
    public static void main (String [] args) {
        Person p = new Person ();
        p.show ();
    }
}

[Run] Results:
Pet (1)
Pet (2)
Pet (3)
--- Person () ---
Pet (33)

show ---- running


The above example, although t1, defined t2, t3 throughout the class, but the initialization sequence by t1, t2, t3 define the order of the decision (DIY swap t1, t2, t3 look at the results), and initialization priority in the implementation of the builder, when calling Person builder, t3 re-initialized.

Initialize static data

If the data is static (static), the same process is also performed. If it is a basic type, but not be initialized, you will automatically receive their standard basic types of the initial value; if it is a handle to an object, unless you create an object linked with it, or else get a null value (null) . If the initialization when defining, for a non-static values ​​take is different, because it is only a static storage area. E.g:

class Bowl {
    Bowl (int marker) {
        System.out.println ( "Bowl (" + marker + ")");
    }
    void f (int marker) {
        System.out.println ( "f (" + marker + ")");
    }
}

class Table {
    static Bowl b1 = new Bowl (1);
    Table () {
        System.out.println ( "Table ()");
        b2.f (1);
    }
    void f2 (int marker) {
        System.out.println ( "f2 (" + marker + ")");
    }
    static Bowl b2 = new Bowl (2);
}

class Cupboard {
    Bowl b3 = new Bowl (3);
    static Bowl b4 = new Bowl (4);
    Cupboard () {
        System.out.println ( "Cupboard ()");
        b4.f (2);
    }
    void f3 (int marker) {
        System.out.println ( "f3 (" + marker + ")");
    }
    static Bowl b5 = new Bowl (5);
}

public class StaticInitialization {
    public static void main (String [] args) {
        System.out.println ( "Creating new Cupboard () in main");
        new Cupboard ();
        System.out.println ( "Creating new Cupboard () in main");
        new Cupboard ();
        t2.f2 (1);
        t3.f3 (1);
    }
    static Table t2 = new Table ();
    static Cupboard t3 = new Cupboard ();
}

[Run] Results:
Bowl (1)
Bowl (2)
Table ()
f (1)
Bowl (4)
Bowl (5)
Bowl (3)
Cupboard ()
f (2)
Creating new Cupboard () in main
Bowl (3)
Cupboard ()
f (2)
Creating new Cupboard () in main
Bowl (3)
Cupboard ()
f (2)
f2 (1)
f3 (1)


Static code block

Java allows other static initialization is divided into class a special code block, this block of code in the form of static keyword, followed by a main method, called static code block. Static code block is executed only in the first generation objects of that class or first visit to belong static member of that class. E.g:

class Person {
    Person (int age) {
        System.out.println ( "Person (" + age + ")");
    }
    void f (int age) {
        System.out.println ( "f (" + age + ")");
    }
}

class Persons {
    static Person p1;
    static Person p2;
    static {
        p1 = new Person (1);
        p2 = new Person (2);
    }
    Persons () {
        System.out.println ( "Persons ()");
    }
}

public class ExplicitStatic {
    public static void main (String [] args) {
        System.out.println ( "Inside main ()");
        Persons.p1.f (18); // 1
    }
    static Persons x = new Persons (); // 2
    static Persons y = new Persons (); // 2
}

P1 when accessing static objects in the row marked 1, line 1 or 2 and the line is a comment not comment is, static initialization module for Persons will run. If 1 and 2 are commented out, the static code block for Persons will not be executed.

Sequence of static properties and static code block execution

class Person {
    Person (int age) {
        System.out.println ( "Person (" + age + ")");
    }
}
class Persons {
    static Person p = new Person (2); // 1
    static {
        p = new Person (3);
    }
    static Person p = new Person (2); // 2

}
public class CompStaticInit {
    public static void main (String [] args) {

    }
    static Persons x = new Persons ();
}

According to Note 1 Reserved 2, Note 2 Reserved 1 results analysis, the execution order of the static properties and static code blocks depends on the coding sequence. Who will perform in front of anyone.

Initialize non-static properties

class Animal {
    Animal (int age) {
        System.out.println ( "Animal (" + age + ")");
    }
    void f (int age) {
        System.out.println ( "f (" + age + ")");
    }
}
public class NotStaticInit {
    Animal a1;
    Animal a2;
    {
        a1 = new Animal (1);
        a2 = new Animal (2);
        System.out.println ( "a1 & a2 initialized");
    }
    NotStaticInit () {
        System.out.println ( "NotStaticInit");
    }
    public static void main (String [] args) {
        System.out.println ( "Inside main ()");
        NotStaticInit x = new NotStaticInit ();
    }
}

Similar to the static code block, anonymous block non-static properties with the initialization sequence depends on the coding sequence.

The succession of object initialization

class Insect {
    int i = 1;
    int j;

    Insect () {
        prt ( "i =" + i + ", j =" + j);
        j = 2;
    }

    static int x1 = prt ( "static Insect.x1 initialized");

    static int prt (String s) {
        System.out.println (s);
        return 3;
    }
}

public class Beetle extends Insect {
    int k = prt ( "Beeklt.k initialized");

    Beetle () {
        prt ( "k =" + k);
        prt ( "j =" + j);
    }

    static int x2 = prt ( "static Bootle.x2 initialized");
    static int prt (String s) {
        System.out.println (s);
        return 4;
    }

    public static void main (String [] args) {
        prt ( "Beetle constructor");
        Beetle b = new Beetle ();
    }
}

[Run] Results:
static Insect.x1 initialized
static Bootle.x2 initialized
Beetle constructor
i = 1, j = 0
Beeklt.k initialized
k = 4
j = 2


Run Java on Beetle, the first thing that happens is that the loader to find out that class. During loading, loading the program finds a base class, along with its load. Whether or not the base class object creation, this process will be executed. If the base class has another base class, then another base class will be loaded, and so on. Next on the implementation of static initialization in the root base class, and then the next derived class is executed, and so on. This is because the initialize derived class may depend on the initialization of the base class members.

When a class is loaded, you can create the object. First, the object of all the basic data types are set to their default values, object handles set to null. Then execute the base class constructor. This situation is done automatically (derived class constructor calls the default super (), you can specify a base class constructor by super). After completion of the base class constructor, the derived class instance variables will be initialized by the original order, and then perform the builder of the remaining body parts.

Summary process objects created:

Static only when the class is loaded executed and performed only once;
Only non-static instantiation executed each time you create an object is executed;
Before the implementation of a static non-static, static base class in the derived class static execution priority;
Execution attributes static properties and static code block depending on their position in the class, who is the former executive who first;
The execution order of non-static properties and building blocks depending on their position in the class, who is the former executive who.
     
         
         
         
  More:      
 
- Programmers Do not neglect debugging techniques (Programming)
- Ubuntu install Lighttpd + Mono support .net (Linux)
- Oracle 11g R2 RAC RMAN backup script example (Database)
- Android use canvas board painting (Programming)
- Windows SmartGit installation (Linux)
- Using IE can also be easily remotely control the computer (Linux)
- Cross server / client backup command: rsync use (Server)
- The sublime into IDE (Linux)
- VMware virtual machine to install CentOS 7 (Linux)
- Ubuntu file security removal tool (Linux)
- Management DB2 logs (Database)
- Using BBED repair ORA-01190 error (Database)
- configuration ssh without password under Linux (Linux)
- Python uses multi-process pool (Programming)
- Linux virtual memory and physical memory (Linux)
- The lambda expression Java8 (constructor references) (Programming)
- Installation and use GAMIT / GLOBK Software (Linux)
- 17 How to install the Ubuntu 14.04 and Linux Mint Kodi14 (XBMC) (Linux)
- Different versions of MongoDB achieve master-slave replication (Database)
- How to install Hadoop on CentOS7 (Server)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.