×

java · level 1 · unit 7 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Big programs are small pieces working as a team.

Remember ? A class is a blueprint. Write a small one of your own inside Main, like static class Pet, and list its fields between curly brackets: the boxes every pet gets, such as name and hunger.

new Pet() builds an from the blueprint, and a dot reaches its fields: rex.name = "Rex";. Objects work with everything you know. Hand one to a method, test it with an if, or join its fields into text.

When something goes wrong, you : find the bug, fix it, run again. Every programmer does this every day. Small pieces help, because you can test each one on its own.

public class Main {
    static class Pet { String name; int hunger; }
    static boolean isHungry(Pet pet) {
        return pet.hunger > 5;
    }
    public static void main(String[] args) {
        Pet rex = new Pet();
        rex.name = "Rex";
        rex.hunger = 8;
        System.out.println(rex.name + " is hungry: " + isHungry(rex));
    }
}

A short class can sit on one line, with a semicolon after each field.