This site is retired and is no longer updated since November 2021. Please visit our new website at https://www.teamscode.org for up-to-date information!
Classes

Vocab Bar
Class A software "blueprint" that describes the data and properties of a type of variable.
Object A single instance of a class – it contains the class’s different properties.

A class is the overall template that includes everything that describes a specific type of variable. Classes consist of a collection of similar methods and variables that together describe the various behaviors of some new thing. An object is a single instance of that class, and contains the class’s various properties/behaviors (which are variables and methods). The following image is an analogy for a class and its instances.


A class and its instances. Source

As you can see from the above image, a class is the blueprint used to create new objects, which can then be used for a variety of different tasks. Objects, especially their differences with primitives, will be covered in more depth in lesson #31 (Objects).

Although you have already been using classes, this lesson will go more in-depth on what makes a class and how a class is used. For example, the statement Math.abs(-2) is a method inside of the Math class. To better understand the concept of classes, let’s look at a new example. Below is a rudimentary class that represents someone’s email account.

    public class EmailAccount {


        private String username;

        private String password;

        // constructor (explained next lesson) that creates new email account

        public EmailAccount(String username, String password) {

            this.username = username;

            this.password = this.password;

            createAccount(username, password);

        }

        public void createAccount(String username, String password) {

            /* implementation code */

        }

        public void sendEmail(String message, String recipient) {

            /* implementation code */

        }

        public void readEmails() {

            /* implementation code */

        }

    }

The above class demonstrates the basic syntax of the class. Here is an image dividing the above class into its main sections:

The class header consists of an access modifier (public/private), the keyword class, and the class name. The class name is structured using CamelCasing, but where the first letter is capitalized (as opposed to lowercase in methods/variables). In the next lesson, we’ll explain the use of fields, constructors, and creating instances of classes.

Lesson Quiz

1. What is a class?

a. A group of topics combined into a single text document.
b. A software "blueprint" that describes the data and properties of an object.
c. A destination where programmers go to collaborate and share code.
d. A list of field variables.

2. What are classes used for?

a. Classes model specific behaviors and store data using methods and variables.
b. Classes are solely used to print things.
c. Classes are used to create new methods and variables.
d. Classes group programs to enhance readability.

Written by Chris Elliott

Notice any mistakes? Please email us at learn@teamscode.com so that we can fix any inaccuracies.