Anatomy of a Class

Emily Wilson
9 min read
Listen to this study note
Study Guide Overview
This study guide covers class writing in Java, including variable declarations, constructors, and methods. It explains the concept of encapsulation and the roles of instance variables and methods. The guide also presents a design problem involving Student and Assignment classes and provides practice problems focusing on correct class structure and access modifiers (public/private).
#What is a Class?
A class in Java can be thought of as a blueprint for an object. Just as a blueprint for a house outlines the design and features of the house, a class in Java outlines the attributes and behaviors of an object. For example, a class for a car might include attributes such as the make, model, and year, as well as behaviors such as driving and honking the horn. When you create an object based on a class, it is like using the blueprint to build a specific instance of the house or car. The object has all of the attributes and behaviors defined in the class, but can also have its own unique characteristics and actions.
#Introduction to Class Writing
Class writing is something you will be doing a lot throughout the rest of AP CSA, and, in fact, there will be one FRQ dedicated to this topic! Class writing involves not just coding a class, but thinking through what needs to be done and how to achieve it as well.
#The Parts of a Class
A class has three main parts: variable declarations, constructors, and methods.
Variable declarations list the variables that are used to characterize objects. Constructors are what create objects, and a class can have multiple constructors. Finally, methods create an effect, either changing other objects, changing itself, or doing something else. Each class is in its own file with the same name. Here is an example of a class:
public class Square {
private int side;
This one line after the class header is the variable declaration. In this class, we have a private integer named side, which represents the side length of the square. We have set this variable to private because we want to restrict access to the data in the class from other classes. Otherwise, anyone could change the data without permission. This has the potential for other classes and programmers to break the code. This the principle of encapsulation, keeping information private and only allowing approved methods to access such data.
After declaring a class and defining the body of the class, you can create objects using this format: Classname objectname = new Classname();
For example, for our Square class, creating a new object could look something like this:
Square mySquare = new Square();
These are the two constructors for the Square class which makes Square objects. The first one is the full constructor where the client (other users and classes) can input parameters into the constructor. The other constructor overloads the first constructor and is the default constructor with default values.
public Square(int sideLength) {
side = sideLength;
}
public S...

How are we doing?
Give us your feedback and let us know how we can improve