Skip to content

Defining classes (class)

A class is defined using the class keyword, followed by the class name and a body {} containing fields and methods.

Syntax:

class ClassName {
// fields
// constructors
// methods
}

Example:

class Person {
String name;
int age;
void speak() {
System.out.println("Hello, my name is " + name);
}
}

Naming conventions:

  • Class names start with an uppercase letter (PascalCase).
  • Use meaningful, descriptive names.

Class modifiers:

  • public – accessible from anywhere (most common)
  • abstract – cannot be instantiated
  • final – cannot be subclassed
  • strictfp – strict floating‑point behavior
public class Employee {
// ...
}