← Java from Scratch Lesson 29 of 88 · 5 XP

Warm-up: a hero warm-up

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

A class describes what an object is made of. An object is created with new, and fields are reached with a dot:

Hero h = new Hero();
h.name = "Kai";
h.hp = 100;
System.out.println(h.name + ": " + h.hp + " HP");   // Kai: 100 HP

The Hero class can go in the same file, after Main — without the word public.

Task. The Hero class is already there. Read a name and health into the object's fields and print name: N HP.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.