We’re off to a good start! We created a Java class, but it currently does not do anything; we need to describe the behavior of the class for it to be useful.

Let’s start by creating the starting state of our class. We can do this by adding a class constructor to it.

  1. A class constructor will allow us to create Dog instances. With a class constructor, we can set some information about the Dog.
  2. If we do not create a class constructor, Java provides one that does not allow you to set initial information.

The code below demonstrates how a class constructor is created:

class Car { //The class constructor for the Car class public Car() { } }

In the example above, we created a class constructor for the Car class. This constructor will be used when we create Car instances later. The public keyword will be explained later in this course.



Add a class constructor called Dog to the class.

Note: If you’re getting an error in the console about a main method, include the following code within the Dog class (it will be explained later in this lesson):

public static void main(String[] args) { }

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?