search

Home  >  Q&A  >  body text

What is the difference between assignment in static properties and assignment in static code blocks?

private static String name = "demo";
private static String name;

static {
    name = "demo";
}

As shown in the above code, what is the difference between the two writing methods, or what are the advantages and disadvantages of each.

高洛峰高洛峰2776 days ago648

reply all(4)I'll reply

  • 世界只因有你

    世界只因有你2017-05-18 10:46:30


    There is no difference as far as the intermediate code is concerned

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:46:30

    There is no difference in the result, but you can write other code in the code block, such as stuffing data into the map.

    reply
    0
  • 某草草

    某草草2017-05-18 10:46:30

    In the static code block, you can assign values ​​to more variables and other operations. It is generally used for initialization, such as loading static resource images when the game is loaded.

    reply
    0
  • PHPz

    PHPz2017-05-18 10:46:30

    There is a small difference: in the same class, the initialization of static properties is completed before the static initialization block

    reply
    0
  • Cancelreply