Where instance variables are initialized

public class Ex { int a; public Ex() { System.out.println("a is "+a); } } 

: a - 0

where a gets initialized ...

I know that the default values ​​for int are zero. The question is, where is it initialized ... through the default constructor? (I heard that the default constructor is created when we do not mention any constructor in the class)

+3
source share
8 answers

If you did not initialize a yourself (this is a primitive value), it is automatically initialized to the default value, 0 in this case.

Read section 4.5.5. (Initial Variable Values) in this document .

+6
source

its default value is 0,

It is not always necessary to assign a value when declaring a field. Fields declared but not initialized will be set to a reasonable default compiler.

enter image description here

+5
source

Definitely not in the default constructor. According to JLS, this happens as part of the evaluation of the class instance creation expression (when you make new ClassName... ) before calling the constructor. From the JLS 3rd edition, 15.9.4 :

Then space is allocated for the new class. If there is not enough space to allocate an object, class evaluation termination of instance creation terminates abruptly by throwing an OutOfMemoryError (§15.9.6).

The new object contains new instances of all fields declared in the specified class type and all of its superclass. As each new field is instantiated, it is initialized to its default value (§4.12.5).

Next, the actual constructor arguments are evaluated, from left to right. If any of the evaluation arguments terminate suddenly, any arguments on the right side are not evaluated, and the class instance of the creation expression terminates suddenly for the same reason.

Next, the selected constructor of the specified class type is called. This leads to the call of at least one constructor for each superclass class type. This process can be directed by an explicit constructor (§8.8) and is described in detail in §12.5.

+3
source

int has a default value of 0

See this link for different default values ​​depending on type. ( Default Values )

+1
source

a is not initialized, so it gives a null value because the default value for int is 0.

+1
source

a has a primitive type int . In your code, a is not initialized to a default of 0 .

+1
source

It is not always necessary to assign a value when declaring a field.
Fields declared but not initialized will be set to a reasonable default compiler. Generally speaking, this default value will be zero or zero, depending on the data type.

 Data Type Default Value (for fields) byte 0 short 0 int 0 long 0L float 0.0f double 0.0d char '\u0000' String (or any object) null boolean false 

PS: Based on such defaults, however, it is usually considered a bad programming style.

+1
source

In java, variable instances will be initialized from the constructor (by default, if you don't have one).

 public class TestFile { String x = null; int y = x.length(); public TestFile() { // TODO Auto-generated constructor stub } /** * @param args */ public static void main(String[] args) { TestFile tf = new TestFile(); } } 

you get stacktrace

 Exception in thread "main" java.lang.NullPointerException at TestFile.<init>(TestFile.java:7) at TestFile.main(TestFile.java:16) 

called inside the constructor. For static fields, initialization when loading classes

+1
source

Source: https://habr.com/ru/post/1338710/


All Articles