Java Specific Area

I am browsing an Android source, just read it, and I met a strange piece of code in Android.Util.JsonReader . It looks like this:

 private final List<JsonScope> stack = new ArrayList<JsonScope>(); { push(JsonScope.EMPTY_DOCUMENT); } 

What does it do? That is, the scope immediately after assigning new ? If I understand correctly, whenever this class, JsonReader is created, (not static, right?), The stack will be initialized here, and not through this.stack = ... in the constructor, right?

What does the scope do? Is stack after stack initialized? I'm just a little confused here as to the name of this template and its use.

+6
source share
1 answer

This is not related to the new statement. This is an initializer, sort of like an unnamed constructor without parameters.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.6

They are executed before class constructors in textual order.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5

+5
source

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


All Articles