skip to content

Class Declaration & Structure

The anatomy of a Java class declaration and the rules about what may live in a file, including the one-public-class-per-file constraint. Easy to take for granted until an interviewer asks why a file will not compile.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What are the components of a Java class declaration, and what does a minimal class look like?

level: juniorimportance: must knowfreq 70%

answer

  1. [modifiers] class Name [extends] [implements] { body }
  2. Minimal: class Dog {}
  3. Top-level modifiers: public/abstract/final/sealed — NOT private/protected
  4. No extends = implicitly extends Object
  5. Body = fields, methods, constructors, nested types

basics

~20 s

A class declaration is the keyword class, a name, and a body in curly braces holding fields and methods. Optionally it has modifiers (like public) before it and an extends/implements clause. Minimal: class Dog { }.

solid answer

~40 s

A top-level class declaration reads: optional modifiers, the class keyword, the class name, an optional generic type-parameter list, an optional extends clause (one superclass), an optional implements clause (one or more interfaces), then a body in braces. The body holds members: fields, methods, constructors, initializer blocks, and nested types. Legal top-level modifiers are public, final, abstract, sealed/non-sealed, and strictfp; private and protected are not allowed on a top-level class. By convention the name is PascalCase. A minimal valid class is just `class Dog {}` — no modifiers, no superclass (it implicitly extends Object), and an empty body. The compiler supplies a default no-arg constructor when you write none.

go deeper

for a junior

Can write a class with the keyword, a PascalCase name, and braces, and add a field and method.

for a middle

Knows the full clause order (modifiers/extends/implements), which modifiers are legal on a top-level class, and that omitting extends means Object.

for a senior

Explains package-private default access, implicit Object superclass, default-constructor generation, and the role of sealed/abstract/final in expressing design intent.

for a principal

Frames class structure in terms of API surface and encapsulation — chooses modifiers deliberately (final/sealed for closed hierarchies) and can teach the grammar precisely.

## What a class is In Java, a **class** is a blueprint that defines the structure (data) and behavior (methods) of objects. Every piece of code you write lives inside a class (or another type like an interface, enum, or record). Understanding the exact shape of a class declaration is the foundation for everything else in object-oriented Java. ## The grammar of a class declaration A **top-level class** (one written directly in a file, not nested inside another type) has this structure, reading left to right: ``` [modifiers] class Name [<TypeParameters>] [extends Superclass] [implements Interface1, Interface2, ...] { body } ``` Term by term: - **modifiers** — optional keywords that change visibility or behavior. For a top-level class the legal ones are: `public` (visible everywhere), `abstract` (cannot be instantiated, may have unimplemented methods), `final` (cannot be subclassed), `sealed`/`non-sealed` (restrict which classes may extend it, Java 17+), and `strictfp` (a now-rarely-used floating-point flag). You may NOT write `private` or `protected` on a top-level class — those only apply to *members* and *nested* classes. - **class** — the keyword that says "this is a class declaration" (as opposed to `interface`, `enum`, or `record`). - **Name** — the identifier. By the universal Java naming convention it is **PascalCase** (each word capitalized, e.g. `BankAccount`). This is a convention, not a compiler rule, but every codebase and tool expects it. - **<TypeParameters>** — optional generics, e.g. `class Box<T>`, letting the class work with a type supplied later. - **extends Superclass** — optional; names the single class this one inherits from. Java allows only **one** superclass (single inheritance of implementation). If you omit it, the class implicitly extends `java.lang.Object`, the universal root of all classes. - **implements ...** — optional; lists one or more interfaces whose contracts this class promises to fulfill. A class may implement many interfaces. - **{ body }** — curly braces enclosing the **members**: fields (variables that hold state), methods (behavior), constructors (initialization logic), static and instance initializer blocks, and nested types. ## The minimal class The smallest legal class is: ```java class Dog {} ``` This is complete and compiles. It has no modifiers (so it has *package-private* access — visible only within its own package), it implicitly extends `Object`, implements no interfaces, and has an empty body. Because you wrote no constructor, the compiler generates a **default no-argument constructor** for you, so `new Dog()` works. ## A fuller example ```java public final class BankAccount { private long balanceCents; // a field (state) public BankAccount(long opening) { // a constructor this.balanceCents = opening; } public long balanceCents() { // a method (behavior) return balanceCents; } } ``` Here `public` makes it visible everywhere, `final` forbids subclassing, the body holds one field, one constructor, and one method. ## Why this matters Knowing the precise anatomy lets you read any class at a glance, predict its visibility and inheritance, and avoid compiler errors like applying `private` to a top-level class. It also clarifies what is mandatory (the keyword, name, and braces) versus optional (everything else).

  • What access does `class Dog {}` have with no modifier?
    Package-private (default): visible only to other code in the same package.
  • What happens if you write no constructor at all?
    The compiler generates a default public no-arg constructor (with the same access as the class), so `new Dog()` works — unless you declare any other constructor, in which case it is not generated.

saying these in an interview costs you the question

  • Saying a top-level class can be private or protected
  • Claiming the class keyword or braces are optional
  • Thinking a class with no extends has no superclass (it has Object)
  • Confusing modifiers with the class name order

context

open as a page

Explain the one-public-class-per-file rule and how a .java file's name relates to its classes.

level: juniorimportance: must knowfreq 68%

basics

~20 s

A single .java file can have at most one public top-level class, and the file must be named exactly after that public class (plus .java). You can add more non-public (package-private) classes in the same file.

open as a page

Within a .java source file, what is the required order of the package statement, imports, and type declarations?

level: middleimportance: should knowfreq 45%

basics

~10 s

A source file has up to one package statement first, then any import statements, then the class (and other type) declarations. The package line must come before imports, and imports before any class.

open as a page

What is the difference between a top-level class and a nested class, and what kinds of nested classes does Java have?

level: middleimportance: should knowfreq 58%

basics

~20 s

A top-level class is declared directly in a file. A nested class is declared inside another class. Java has four nested kinds: static nested classes, inner (non-static) classes, local classes (inside a method), and anonymous classes.

open as a page

What kinds of members can a class body contain, and which modifiers apply to members but not to a top-level class?

level: seniorimportance: should knowfreq 40%

basics

~10 s

A 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.

open as a page