Ref: Initializing Fields
Instance initializers are permitted to refer to the current object via the keyword this,Ref:
Instance Initializers
1 /* 2 Initializer blocks,have the same capability as 'constructor',that is, they can use to initialze instance members. 3 We use 'initialzer blocks',because all the instances of the class can have the same characteristic,such as 'cry'. 4 */ 5 package kju.obj; 6 7 import static kju.print.Printer.*; 8 class InitializerBlock { 9 public static void main(String[] args) { 10 Person p = new Person(); 11 Person lily = new Person("Lily", 21); 12 printHr(); 13 lily.cry(); 14 /*prints begin: 15 name = null is crying 16 name = null is crying 17 18 ----------------------------------- 19 name = Lily is crying 20 :prints ends*/ 21 } 22 } 23 24 class Person { 25 { 26 cry(); 27 } 28 29 public Person() { 30 31 } 32 33 public Person(String name) { 34 this.name = name; 35 } 36 37 public Person(String name, int age) { 38 this(name); 39 this.age = age; 40 } 41 42 public void cry() { 43 println("name = " + name + " is crying"); 44 } 45 46 private String name; 47 private int age; 48 }