zuai-logo

Writing Classes

Caleb Thomas

Caleb Thomas

11 min read

Listen to this study note

Study Guide Overview

This study guide covers writing classes in Java, including: class anatomy (variables, methods), constructors, documentation with comments, accessor and mutator methods, writing methods (with pass-by-value considerations), static variables and methods, scope (global and local), the this keyword, and ethical and social implications of computing systems.

This unit makes up 5-7.5% of the AP exam, and FRQ #2 requires you to write a class from scratch. Learning how to write a class opens up many doors to creating programs that solve a variety of problems. You could write a class to simulate a candy store, with methods for the cart and converting candies to their prices; a class to create a calculator interface that can solve more than just your average calculator, with methods for the Pythagorean theorem and the area of a trapezoid; and even a class to simulate a bank account, with variables for the balance and account holder name, and methods to withdraw or deposit money. This guide will give you an overview of the important components of writing a class.

5.1 Anatomy of a Class

class is a blueprint for the objects of that class. A class contains variables and methods to store and manipulate information. To create a class, you first state whether you want it to be public or private, use the class keyword, and name the class. Then, you add a set of curly braces {} that will contain the contents of the class. To understand the key components of classes, we will create an AreaCalculator class. This is the class header:

public class AreaCalculator {}

Inside the curly braces, you can define the variables and methods of the class. Variables are used to store data, and methods are used to perform actions. In the AreaCalculator class, we can create an instance variable to store the number of shapes a user has asked to calculate the area for:

public class AreaCalculator {

private int numShapes;

}

We made this variable private because other classes in this program will not need to access this variable. For the most part, instance variables will be private. The class itself, however, needs to be public so other classes can create an object of the AreaCalculator.

5.2 Constructors

Notice how we only declared the variable numShapes, but we haven't initialized it yet. To initialize instance variables, we need to create a constructor. A constructor in Java is a special method that is used to create and initialize an object of that class. Every class must have at least one constructor, and you can define additional constructors as well. If you do not define a constructor for your class, a default one will be created.

A constructor has the same name as the class, and it does not have a return type. Constructors should be made public so objects of a class can be created in other classes. Inside a constructor, we can initialize the numShapes instance variable.

public class AreaCalculator {

private int numShapes;

public AreaCalculator() {

numShapes = 0;

}

}

A constructor can take in parameters as well. It was not necessary to take in a parameter for this example, but here is an example of how constructor parameters are used to initialize the instance variables:

public class Car {

private String brand;

private String model;

private int year;

public Car (String make, String carModel, String yearMade) {

brand = make;

model = carModel;

year = yearMade;

}

}

In this example, the parameters of the constructor are being assigned to the instance variables to initialize the instance variables.

5.3 Documentation with Comments

Other people who read your class, such as your teacher or your friends, may not understand what the variables and methods in your class are for. That's why it's important to document your...

Question 1 of 15

🎉 What is the basic structure for declaring a class in Java?

class MyClass;

public class MyClass()

public class MyClass {}

class {MyClass};