Classes & Constructors in Java

From the Java cheat sheet · Classes & Records · verified Jul 2026

Classes & Constructors

Fields, constructors, this, and access modifiers.

java
// Quick Reference
class Person {
    private String name;
    Person(String name) { this.name = name; }
    String getName() { return name; }
}
var p = new Person("Sam");
💡 this refers to the current object - it disambiguates fields from parameters.
⚡ this(...) chains to another constructor in the same class to avoid duplication.
📌 Access modifiers: private (class), default (package), protected (subclass), public.
🟢 Keep fields private and expose behavior through methods (encapsulation).
classesconstructors

More Java tasks

Back to the full Java cheat sheet