Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Thinking in Java study notes - initialization and cleanup     - Linux usage in echo (Linux)

- DataGuard Standby backup error RMAN-06820 ORA-17629 to solve (Database)

- Linux use glibc2 library and crypt () function to generate the user password (Programming)

- MD5 and simple to use (Linux)

- Java and C / C ++ data conversion when network communication (Programming)

- Zabbix monitoring of the switch (Server)

- How to enable Software Collections (SCL) on CentOS (Server)

- Teach you how to synchronize Microsoft OneDrive in Linux (Linux)

- CentOS 7 How to connect to a wireless network (Linux)

- Dom4j change XML coding (Programming)

- Linux operating system ARP Spoofing Defense (Linux)

- Sublime Text 3 best features, plug-ins and settings (Linux)

- Nginx configuration support f4v video format player (Server)

- Android recyclerview cardview (Programming)

- Oracle row and column switch to turn columns (Database)

- Linux Detailed instructions alias settings (Linux)

- Install the latest Eclipse IDE in Ubuntu (Linux)

- Linux security configuration (Linux)

- MySQL and Oracle time zone settings compare (Database)

- Three details reflect the Unix system security (Linux)

 
         
  Thinking in Java study notes - initialization and cleanup
     
  Add Date : 2018-11-21      
         
         
         
  Member initialization

Java try to ensure that: all variables before use can be properly initialized.

Local variables: the uninitialized compilation errors.
void f () {
        int i;
// System.out.println (i); // compile error
    }
Data members of the class (fields) are basic types: to ensure there will be an initial value.
public class InitialValues {
    boolean bl;
    char c;
    byte bt;
    short s;
    int i;
    long l;
    float f;
    double d;
    InitialValues reference;
    void printValues () {
        System.out.printf (
                "\ Nboolean:" + bl
                + "\ Nchar:" + c
                + "\ Nbyte:" + bt
                + "\ Nshort:" + s
                + "\ Nint:" + i
                + "\ Nlong:" + l
                + "\ Nfloat:" + f
                + "\ Ndouble:" + d
                + "\ Nreference:" + reference
        );
    }

    public static void main (String [] args) {
        new InitialValues () printValues ().;
    }
}
Results output:

boolean: false
char:
byte: 0
int: 0
long: 0
float: 0.0
double: 0.0
reference: null
Specifies initialization
In the definition of the class member variables place for variable assignment

public class InitialValues {
    boolean bl = true;
    char c = 'a';
    byte bt = 22;
    short s = 0xff;
    int i = 1202;
    long l = 1;
    float f = 3.14f;
    double d = 3.14159;
}
Initialize the object in the same way non-basic types

class Apple {}
public class InitialValues {
    Apple apple = new Apple ();
}
Call the method to the initial value

    int i1 = getIntValue ();
    int i2 = getDoubleValue (2);

    private int getDoubleValue (int i) {
        return i * 2;
    }

    private int getIntValue () {
        return 2;
    }
Constructor to initialize

Were unable to prevent automatic initialization, it will occur before the constructor is called.

public class ConstructorInitial {
    int i;
    ConstructorInitial () {
        System.out.println (i);
        i = 1;
        System.out.println (i);
    }

    public static void main (String [] args) {
        new ConstructorInitial ();
    }
}
result:

0
1
The initialization sequence
Within the class, order of variable definition determines the order of initialization.

Initialize static data
No matter how many objects are created, static data occupies only a storage area.
Static initialization only when necessary will be carried out.
Initialization sequence is: Static objects -> non-static objects.

The process of object creation
Suppose there is a class called Dog:

When you first create Dog object (constructor can be viewed as a static method) or the first visit to a static method or static field Dog class, Java interpreter must locate the class path to locate Dog.class file.
Then load Dog.class (This will create a Class object), all actions are performed on static initialization. Thus, static initialization only when the Class object is first loaded once.
When you create an object with new Dog (), the first allocated on the heap enough storage space for the Dog object.
This storage will be cleared, which automatically Dog object of all the basic types of data are set to default values (the number is 0, boolean type, similar to the character), and the reference is set to null.
Perform all appeared in the fields defined at initialization action.
Executive constructor.
Static initialization display
Java allows multiple static initialization action organized into a special "static block"

class Cup {
    Cup (int marker) {
        System.out.println ( "Cup (" + marker + ")");
    }
    void f (int marker) {
        System.out.println ( "f (" + marker + ")");
    }
}
class Cups {
    // Static member variables
    static Cup cup1;
    static Cup cup2;
    // Explicit static initialization
    // Static block
    static {
        System.out.println ( "static initialization");
        cup1 = new Cup (1);
        cup2 = new Cup (2);
    }
    Cups () {
        System.out.println ( "Cups ()");
    }
}
public class Initialization {
    public static void main (String [] args) {
        Cups.cup1.f (100);
        / **
         * Static initialization
         Cup (1)
         Cup (2)
         f (100)
         * /
    }
// Static Cups cups = new Cups ();
    / **
     * Static initialization
     Cup (1)
     Cup (2)
     Cups ()
     * /
}
Non-static instance initialization
Initializes an instance: non-static variables are initialized for each object.

class Cup {
    Cup (int marker) {
        System.out.println ( "Cup (" + marker + ")");
    }
    void f (int marker) {
        System.out.println ( "f (" + marker + ")");
    }
}
class Cups {
    // Static member variables
    static Cup cup1;
    static Cup cup2;
    Cup cup3, cup4;
    // Explicit static initialization
    // Static block
    static {
        System.out.println ( "static initialization");
        cup1 = new Cup (1);
        cup2 = new Cup (2);
    }
    // Non-static instance initialization
    {
        System.out.println ( "non-static initialization");
        cup3 = new Cup (3);
        cup4 = new Cup (4);
    }
    Cups () {
        System.out.println ( "Cups ()");
    }
}
public class Initialization {
    public static void main (String [] args) {
        new Cups ();
        / **
         * Static initialization
         Cup (1)
         Cup (2)
         non-static initialization
         Cup (3)
         Cup (4)
         Cups ()
         * /
    }
}
     
         
         
         
  More:      
 
- To achieve a two-way static NAT stateless available modules on Linux (Linux)
- Oracle11g CRS-0184 Problem Solving (Database)
- RedHat command line and graphical interface switching (Linux)
- Ubuntu Slingscold (Linux)
- To install the latest version of Shotwell 0.18 under Ubuntu (Linux)
- Detailed PHP code optimization [section] (explain reasons) (Programming)
- Empty password Linux operating system (Linux)
- Use top to monitor the remote server (Server)
- Improve WordPress performance (Server)
- MySQL uses mysqld_multi to deploy stand-alone multi-instance detail procedures (Database)
- Memcache explain in detail (Server)
- CentOS 6.6 running level (Linux)
- How to set up HTTPS policies for older browsers (Server)
- MySQL In can not be overridden with an internal connection (Database)
- Linux stand-alone OGG synchronous Oracle 11g DB test (Database)
- C ++ thread creates transmission parameters are changed (Programming)
- CentOS 6.5 configuration SSDB 1.8.0 (Server)
- Five Linux user space debugging tool (Linux)
- Awk include binding capacity larger than the specified size of all files directory (Linux)
- To install and use the Doxygen under Linux (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.