Task 2 of 10 · 0 solved · 10 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

4.1 Objects and fields — objects and classes

Lesson

Classes and objects

A class is a blueprint that describes what its objects have and can do. An object is one thing built from that blueprint. The data an object holds lives in named fields (also called instance variables). For example, a Dog has a name and an age.


Worked example

class Dog {
    String name;   // a field
    int age;       // a field
}

This class says every Dog object will have a name and an age. It is a blueprint — no dog exists yet until we build one.


▶ Try it: the Dog class is defined for you. Run this to build a dog and fill in its fields, then change the values and re-run.



A class is a ___, and an object is a ___.


In class Dog { String name; int age; }, what are name and age?


Match each term to what it means.


True or false?


Type exactly what this program prints.

class Cat {
    String name;
}

public class Main {
    public static void main(String[] args) {
        Cat c = new Cat();
        c.name = "Tom";
        System.out.println(c.name);
    }
}