JAVA · ОСНОВЫ · LESSON 19

Drawing and object

Class and object · about 16 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Class - drawing

The class describes what data the subject will have. For example, a cat has a name. We can make a drawing of Cat.

class Cat {
    String name;
}

✦ It's not a specific cat yet.

STEP 2

Create an object.

The word new creates an object from a drawing. Through a dot, you can put a name in his field and read it.

Cat cat = new Cat();
cat.name = "Рыжик";
System.out.println(cat.name);
From blueprint to cat
Catdrawingnew Cat()creationcat.namecat-name

✦ There's Red on the screen.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the signs and brackets.

public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.name = "Рыжик";
        System.out.println(cat.name);
    }
}
class Cat {
    String name;
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create a Dog class with the String name field. In main, create a dog object, put the word «Бим» in its name, and output this field.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework