Static blocks are handy bits of code that let you define one block of code and have it be included in every constructor. Let’s consider an example (in the form of a different class) in Listing 2-17.

Listing 2-17. Static block example

package com.bryantcs.examples;

public class StaticBlockClass {

private static String name; private String whichOne;

static { name = “StaticBlockClass”; } public StaticBlockClass(int identifier) { whichOne = Integer.toString(identifier); }

public StaticBlockClass(String identifier) { whichOne = identifier; }

} – Java 7 for Absolute Beginners


Stay current with the latest in tech comm
Keep current with the latest trends in technical communication by subscribing to the I'd Rather Be Writing newsletter. 4,500+ subscribers