Programming foundations

Stage 1 · Lesson 6 of 9

Classes and objects

Your first language

In Java all code lives in a class, so you meet on day one. A class is a blueprint. It defines fields (data) and methods (behaviour).

new Book("Dune", 412) creates an object from it and runs its constructor, the setup code. Keep fields and give callers methods for what they need. That way the object controls its own rules, such as never allowing a negative page count.

This idea is called encapsulation. static members belong to the class itself rather than to each object, which is why a classic main is static. Override toString() so printing an object shows something useful.

, such as record Point(int x, int y) {}, give you a small data class that cannot change, in one line. Design with classes comes in a later stage; here, learn the mechanics.

A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

  • CourseFree
    Java Programming MOOC

    University of Helsinki · Beginner

    Free Helsinki course with hundreds of auto-tested exercises; parts 1 to 8 cover this stage

  • GuideFree
    Dev.java: Learn Java

    Oracle · Beginner

    Official tutorials from the Java team: read the language basics path next to the MOOC

Practise

  • Small exercises in your language track, with free feedback from a human mentor

Keep handy

  • Quick syntax lookups and an online compiler for testing a snippet on a shared computer

Mark this stage as not done?