Java Reference ยท Keywords and Statements
The class keyword
Reference for the Java class keyword: declare fields, constructors and methods, use access modifiers, nested and anonymous classes, and understand initialization order.
class declares a new reference type: a blueprint that describes the data (fields) and behavior (methods) of its objects, plus constructors that set up new instances. Almost everything you write in Java lives inside a class.
A top-level class can be public (one per file, file name must match) or package-private. Classes can also be nested inside other classes, declared locally inside a method, or created anonymously.
public class Point { private final int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int x() { return x; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
[annotations] [modifiers] class Name [<T>] [extends Parent] [implements A, B] [permits C, D] { // fields, constructors, methods, initializer blocks, nested types }
| Part | Description |
|---|---|
| Modifiers | public, abstract, final, sealed, non-sealed, static (nested only), strictfp (no effect since Java 17). |
| Name | Conventionally UpperCamelCase. |
extends |
At most one superclass; defaults to Object. |
implements |
Any number of interfaces. |
| Body members | Fields, methods, constructors, initializer blocks, nested classes, interfaces, enums and records. |
Usage notes
- Default constructor: if you write no constructor, the compiler adds a public no-argument one (with the class's access level). Declaring any constructor removes it.
- Initialization order: static initializers (once, at class initialization); then for each
new: superclass constructor chain, instance field initializers and initializer blocks in source order, then the constructor body. - Object identity:
==compares references. Overrideequals()andhashCode()together for value comparison; or use arecord. - Encapsulation: make fields
privateand expose behavior, not raw data. Prefer immutability withfinalfields. - Inner classes: a non-static nested class keeps a hidden reference to its outer instance. Use
staticnested classes unless you need that link. - Anonymous and local classes can capture only final or effectively final local variables.
- One public class per file, and its name must equal the file name. Package-private classes can share a file.
- Java 25: a source file may declare
void main()and other members at top level; the compiler wraps them in an implicitly declared class (compact source files, final in Java 25).
Related
abstract,interface,record,new,thisandsuper- Lessons: Classes and objects, Constructors, Nested classes