CodeBlanks Logo Icon CodeBlanks

Summary - Classes

Combine attributes and methods to write a User class.

Summary - Classes

In this group you learned to define a class, add fields and methods, instantiate with new and print objects readably with toString. A class consists of a name, fields and methods. Instances are independent; each carries its own data.

Now you will bring it all together: define a class, add fields and a method, create an instance and print it with toString. This is the cornerstone of object-oriented programming; later, constructors, inheritance and interfaces will build on this foundation.

Practice Define the Car class with toString

Define a class named Car. Add String brand and int year fields. Add a void start() method; its body writes System.out.println(brand + " started");. Add a public String toString() method returning brand + " " + year. In main create a Car instance, assign values to the fields, call the start method and print the instance with println.

Starter code

Main.java
// Define the Car class
// Add fields, a method and toString
// Instantiate, call and print in main

Sample solution

Main.java
class Car {
    String brand;
    int year;
    void start() {
        System.out.println(brand + " started");
    }
    public String toString() {
        return brand + " " + year;
    }
    public static void main(String[] args) {
        Car c = new Car();
        c.brand = "Toyota";
        c.year = 2024;
        c.start();
        System.out.println(c);
    }
}

Expected output

Toyota started
Toyota 2024

What comes next?

You learned the basic class structure. In the next groups you will use constructors to initialize fields, inheritance to share code between classes and interfaces to define contracts.

This group combined field declaration, method definition, instantiation with new and overriding toString. These are the core of object-oriented Java. Next you will explore a cleaner way to set initial values with constructors; instead of assigning fields by hand, you will create instances with a constructor call.

Practice Define the Student class

Define a class named Student. Add String name and int grade fields. Add a public String toString() method returning name + " - " + grade. In main create two Student instances, assign different values and print both with println.

Starter code

Main.java
// Define the Student class
// Create two instances and print

Sample solution

Main.java
class Student {
    String name;
    int grade;
    public String toString() {
        return name + " - " + grade;
    }
    public static void main(String[] args) {
        Student a = new Student();
        a.name = "Alex";
        a.grade = 90;
        Student b = new Student();
        b.name = "Sam";
        b.grade = 85;
        System.out.println(a);
        System.out.println(b);
    }
}

Expected output

Alex - 90
Sam - 85

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Two instances are created from a class. Their fields hold different values. What does this show?

  • The fields are static
  • The instances are independent Correct answer
  • The class is wrongly defined
  • toString is missing

Explanation: The instances are independent — each carries its own field value.

2 Complete the instantiation line

Question
Car c = ____;
c.start();
Answer
Car c = new Car();
c.start();

Explanation: new Car() creates a new car instance.

All lessons in Intermediate