Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid code repetition 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's ..
   //...
}

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);
}

}

like image 388
Hernán Eche Avatar asked Dec 04 '22 13:12

Hernán Eche


2 Answers

Here is your code with 4 different ways of initializing final variables.

  1. inline
  2. anonymous initializer block
  3. initialized in the constructor
  4. calling the default constructor explicitly

The resulting output 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's ..
        // ...
    }

    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
like image 193
crowne Avatar answered Mar 05 '23 06:03

crowne


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

  • instance variable initialization
  • initialization blocks { .. }

So you don't have to include this everywhere just place it either as an instance-variable initialization:

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

or in initialization block:

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

Of course, in this precise example, you should make the field static.

like image 45
Bozho Avatar answered Mar 05 '23 05:03

Bozho