Research on initialization of Java variables

Source: Internet
Author: User

For Java variables, we know that there are member variables and local variables.

About their initialization, basically all the books will be written. Member variables, Java will help you initialize, local variables, you need the programmer to initialize themselves.

The member variable for the class. The Java virtual machine automatically initializes it to its default value, regardless of whether the program has a display initialization.

The rules are:

1, the default value for the basic type variable of the integer type (byte, short, int, long) is 0.

2. The default value of the basic type variable for single-precision floating-point (float) is 0.0f.

3. The default value of the basic type variable for double-precision floating-point type (double) is 0.0d.

4. The default is "/u0000" for basic type variables of character type (char).

5. The default value of the Boolean basic type variable is false.

6. A variable of reference type is a default value of NULL.

7, the default value of the variable of the array reference type is NULL. Spring after an instance of a key array variable, if no value is displayed for each element, Java initializes all elements of the array to its default value of the corresponding type.

Local variable initialization

After a local variable is declared, the Java virtual machine does not automatically initialize it to its default value. Therefore, for a local variable, it must be initialized before it can be used.

If the compiler confirms that a local variable may not be initialized before it is used, the compiler will make an error.


So, how is the modified Java member variable initialized? When was it? Where in the program?

Here's a little program.

public class TESTC {
/*
* Define member variables
* The mantissa is 1 for initialization assignment when defined
* Mantissa = 2 indicates initialization assignment in code block
* The mantissa is 3 to initialize the assignment in the constructor
* The mantissa is 4 to initialize the assignment in a static code block
* The mantissa is 5 to indicate an uninitialized assignment
*/
/*
* Normal member variables
*/
int field_a1 = 5;
int field_a2;
int field_a3;
Error: Non-static variables can no longer be used in static code blocks. Cannot make a static reference to the Non-static field FIELD_A4
int field_a4;
int field_a5;
/*
* Final member variable
*/
Final int field_b1 = 5;
final int field_b2;
final int field_b3;
Error: Non-static variables can no longer be used in static code blocks. Cannot make a static reference to the Non-static field FIELD_B4
final int field_b4;
Error: not initialized. The blank final field field_b5 may not be been initialized
final int field_b5;
/*
* Static member variable
*/
static int field_c1 = 5;
static int field_c2;
static int field_c3;
static int field_c4;
static int field_c5;
/*
* Static Final member variable
*/
static final int field_d1 = 5;
Error: not initialized. The blank final field field_d2 may not be been initialized
static final int field_d2;
Error: not initialized. The blank final field field_d3 may not be been initialized
static final int field_d3;
static final int field_d4;
Error: not initialized. The blank final field field_d5 may not be been initialized
static final int field_d5;

code block
{
FIELD_A2 = 5;
FIELD_B2 = 5;
FIELD_C2 = 5;
FIELD_D2 = 5;
}

Static code block
static{
FIELD_A4 = 5;
FIELD_B4 = 5;
FIELD_C4 = 5;
FIELD_D4 = 5;
}

constructor function
Public Testc () {
FIELD_A3 = 5;
FIELD_B3 = 5;
FIELD_C3 = 5;
FIELD_D3 = 5;
}
}

Then we decompile the. class file generated by the program to see how he works.
Here is the Testc.jad file. Anti-compilation files


[HTML]View Plaincopy
  1. <span style="Font-family:microsoft yahei;font-size:18px;" >public class TESTC
  2. {
  3. Public TESTC ()
  4. {
  5. field_a1 = 5;
  6. field_a2 = 5;
  7. field_c2 = 5;
  8. field_a3 = 5;
  9. field_c3 = 5;
  10. }
  11. int field_a1;
  12. int field_a2;
  13. int field_a3;
  14. int field_a5;
  15. Final int field_b1 = 5;
  16. Final int field_b2 = 5;
  17. Final int field_b3 = 5;
  18. static int field_c1 = 5;
  19. static int field_c2;
  20. static int field_c3;
  21. static int field_c4 = 5;
  22. static int field_c5;
  23. static final int field_d1 = 5;
  24. static final int field_d4 = 5;
  25. }</span>



See here we have a clear idea.

For non-decorated normal member variables, the system assigns values to the constructor by default, regardless of where we assign them.

For final variables, regardless of where we are assigned, the default final variable is initialized in the class.

For static, the system initializes at different locations according to our needs.


Through the error, we can find.

The final variable must be initialized. Otherwise, a compilation error is reported. The blank final field field_d5 may not be been initialized

Initialization of a static member variable occurs when the class is loaded by the class loader (ClassLoader), and the static member variable is assigned the default value in the static zone.

The initialization of an ordinary member variable occurs when the JVM makes a default initialization assignment when it opens up space for the class generation instance .

Research on initialization of Java variables

Related Article

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.