---
title: "Java: Static blocks"
date: 2015-01-01
description: "Tom Johnson’s blog on technical writing and AI: agent skills, docs for AI agents, docs-as-code, tech comm careers, and an API documentation course."
canonical_url: https://idratherbewriting.com/java-static-blocks/index
---

> For AI agents: a documentation index is available at https://idratherbewriting.com/llms.txt. Markdown versions of all pages are available by appending .md to any page URL.

# Java: Static blocks

> 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
