What kinds of members can a class body contain, and which modifiers apply to members but not to a top-level class?
answer
- Members: fields, methods, constructors, initializer blocks, nested types
- Top-level class: only public or package-private; members: all four
- static and private/protected are member-only (vs top-level class)
- final: field=no reassign, method=no override, class=no subclass
- transient/volatile (fields), synchronized/native (methods) are member-only
basics
~10 sA class body can hold fields, methods, constructors, initializer blocks, and nested types. Members can use private and protected and static, which are not allowed on a top-level class.
solid answer
~40 sA class body contains members: fields (instance and static), methods, constructors, instance and static initializer blocks, and nested types (classes, interfaces, enums, records). Members support the full visibility range — private, protected, package-private, and public — whereas a top-level class may only be public or package-private. Members can also be static (belonging to the class rather than an instance), which is meaningless on a top-level class. final on a field forbids reassignment, on a method forbids overriding, and on a class forbids subclassing. abstract applies to methods (no body) and classes. Other member-only modifiers include transient and volatile on fields and synchronized/native on methods. Constructors take a restricted modifier set (the four access levels only). Initializer blocks run during object creation (instance) or class loading (static).
go deeper
Lists fields, methods, and constructors as class members and knows private hides a field.
Adds initializer blocks and nested types, and knows static and private/protected apply to members but not a top-level class.
Explains final's three meanings, abstract on classes vs methods, member-only modifiers (transient/volatile/synchronized/native), and the constructor modifier restrictions.
Uses the modifier matrix to drive encapsulation and API design, reasons about thread-safety (volatile/synchronized), serialization (transient), and class-loading order via static initializers.
## Members of a class The **body** of a class — the part between the braces — holds its **members**. There are five categories: 1. **Fields** — variables that hold state. Instance fields belong to each object; `static` fields belong to the class and are shared by all instances. 2. **Methods** — named blocks of behavior, instance or `static`. 3. **Constructors** — special initialization routines invoked by `new`. They have the class's name and no return type. 4. **Initializer blocks** — unnamed blocks that run during initialization. An **instance initializer** `{ ... }` runs each time an object is created (before the constructor body proper); a **static initializer** `static { ... }` runs once when the class is loaded. 5. **Nested types** — classes, interfaces, enums, or records declared inside the class. ## Modifiers: top-level class vs members This is the crux of the question. The set of legal modifiers **differs** between a top-level class and its members. ### Access modifiers - A **top-level class** may be only **`public`** or **package-private** (no modifier). It may NOT be `private` or `protected`. - A **member** (field, method, constructor, nested type) may use **all four** access levels: `private` (same class only), package-private (same package), `protected` (same package + subclasses), and `public` (everywhere). The reason: `private`/`protected` describe visibility *relative to an enclosing type*, which a top-level class does not have. ### `static` - `static` is meaningful only on **members**: a static field/method/nested class belongs to the class itself, not to an instance. A **top-level class cannot be `static`** — there is nothing for it to be static *within*. (A nested class can be static.) ### `final` `final` has a meaning in three places: on a **field** it forbids reassignment after initialization; on a **method** it forbids overriding by subclasses; on a **class** (top-level or nested) it forbids subclassing. So `final` is shared, but its precise effect depends on where it sits. ### `abstract` `abstract` applies to **classes** (cannot be instantiated, may declare unimplemented methods) and to **methods** (declared with no body, to be implemented by subclasses). It is mutually exclusive with `final` (a final abstract class could never be completed). ### Member-only modifiers Several modifiers exist **only** for members, never a class: - `transient` (field) — excluded from default serialization. - `volatile` (field) — enforces visibility/ordering guarantees across threads. - `synchronized` (method) — acquires the object's (or class's) monitor lock. - `native` (method) — implemented in non-Java code via JNI. - `default` (method, in interfaces) — provides a body in an interface. ### Constructors Constructors accept only the **four access modifiers**. They cannot be `static`, `final`, `abstract`, or `synchronized`, because those concepts don't apply to a one-time initialization routine that is never inherited or overridden. ## Example tying it together ```java public final class Counter { // class: public + final private static int created; // static private field private final int id; // final instance field static { created = 0; } // static initializer { id = ++created; } // instance initializer public Counter() {} // constructor: access modifier only public synchronized int id() { // method: public + synchronized return id; } } ``` ## Why it matters Knowing which modifiers are legal where prevents a class of compile errors and, more importantly, lets you reason about visibility and design intent: `private` fields with `public` accessors are the basis of encapsulation; `final`/`abstract` express extensibility decisions; `static` separates class-level from instance-level state.
- Why can't a top-level class be static?static means 'belonging to the enclosing type rather than an instance'. A top-level class has no enclosing type, so static is meaningless; only nested classes can be static.
- What modifiers can a constructor take?Only the four access modifiers (public/protected/package-private/private). Not static, final, abstract, synchronized, or native — those don't apply to a one-time, non-inherited initializer.
saying these in an interview costs you the question
- Saying a top-level class can be static or private
- Thinking constructors can be final, static, or abstract
- Believing final means the same thing on a field and a class
- Forgetting initializer blocks are class members