How to avoid repeating code initializing final properties?

public class Code{ //many properties //... final String NEWLINE;// ohh a final property! void creation() //this method is for avoid repetition of code { //final initialization can't be put here =( Source= new StringBuffer(); //many other commons new .. //... } Code() { NEWLINE = System.getProperty("line.separator"); creation(); } Code(String name, int numberr) { NEWLINE = System.getProperty("line.separator"); creation(); name=new Someting(name); number = new Magic(number); } 

}

+4
source share
5 answers

Here is your code with 4 different ways to initialize final variables.

  • in-line
  • anonymous initialization block
  • initialized in the constructor
  • explicitly call the default constructor

The resulting result is shown below.

 public class Code { // many properties private String name; private String number; // ... // 1. final String NEWLINE_1 = "1" + System.getProperty("line.separator"); final String NEWLINE_2; final String NEWLINE_3; // 2. { System.out.println("initializer block invoked before Constructor"); NEWLINE_2 = "2" + System.getProperty("line.separator"); // final initialization CAN be put here =( // Source = new StringBuffer(); // many other commons new .. // ... } Code() { System.out.println("default constructor"); // NEWLINE_1 = "error"; can't do this // NEWLINE_2 = "error"; can't do this // 3. NEWLINE_3 = "3" + System.getProperty("line.separator"); } Code(String name, int number) { // 4. this(); System.out.println("constructor(name, number)"); name = new String("Someting(name)"); this.number = new String("Magic(number)"); } public static void main(String[] args) { Code code_1 = new Code(); System.out.println(code_1.NEWLINE_1 + ":" + code_1.NEWLINE_2 + ":" + code_1.NEWLINE_3); Code code_2 = new Code("crowne", 2); System.out.println(code_2.NEWLINE_1 + ":" + code_2.NEWLINE_2 + ":" + code_2.NEWLINE_3); } } 

 initializer block invoked before Constructor default constructor 1 :2 :3 initializer block invoked before Constructor default constructor constructor(name, number) 1 :2 :3 
+6
source

All initializers are added by the compiler to the beginning of each constructor. It includes:

  • initialization of instance variables
  • initialization blocks { .. }

Therefore, you do not need to include this everywhere, just put it either as an initialization of the instance variable:

 private final String NEWLINE = System.getProperty("line.separator"); 

or in the initialization block:

 { NEWLINE = System.getProperty("line.separator"); } 

Of course, in this exact example you have to make the field static .

+6
source

Just do:

 final String NEWLINE = System.getProperty("line.separator"); 

See: JLS 8.3.2. Field initialization .

See also: JLS 12.5 Creating Instances of a New Class for Run Order.

+3
source

If they are initialized the same every time, you can put the code outside the constructors. Java allows you to:

 final String NEWLINE = System.getProperty("line.separator"); 

You can also have all the constructors other than the no-argument argument to invoke the constructor without arguments. For instance:

 Code(String name, int number) { this(); name=new Someting(name); number = new Magic(number); } 
+1
source

Another, if the initialization is complicated and you have to do it at build time, provide a static method, returns the result, as in:

 Code() { NEWLINE = newLineValue(); creation(); } Code(String name, int number) { NEWLINE = newLineValue(); creation(); name = new Something(name); number = new Magic(number); } private static String newLineValue() { return System.getProperty("line.separator"); } 

In this case, newLineValue() trivial, so I would not use it here, but if there really was significant work, then it could be useful. You can also pass parameters from the constructor.

+1
source

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


All Articles