Skip to content
Tutorials Center
My learning

Topics

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.

Java
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

Java Fields, constructors and methods

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.

Java Initialization order and kinds of classes

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

Java
[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. Override equals() and hashCode() together for value comparison; or use a record.
  • Encapsulation: make fields private and expose behavior, not raw data. Prefer immutability with final fields.
  • Inner classes: a non-static nested class keeps a hidden reference to its outer instance. Use static nested 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).