Classes should be written in the order below.

  1. Class header & all public method headers
  2. Instance variables
  3. Code inside methods

This is true for code in actual projects and for AP CS A Free Response that ask students to write an entire class. To follow the order above when responding on paper, see the section below titled “When responding on paper”.

The examples below are from the Coordinate2D class.

Step 1: Class header & all public method headers

The class header and public method headers define how the class can be used by other classes. Most AP CS A Free Response that ask for an entire class provide examples of that class being used by a client class. In recent free response, these examples have been provided in the form of a table. Past free response that request an entire class are linked near the bottom of this page.

If the class extends another class, include this in the class header.

When writing a method header, include the access specifier, return type (for methods other than constructors), method name, and complete parameter list. Don’t write half of a method header then return to it later.

If you aren’t sure about a method header, start with the access specifier and the method name, then add the parameters, then add the return type.

Example 1

public void setX(int newX)

Access specifier: public
Method name: setX
Parameter: int newX
Return type: void

Example 2

public int getY()

Access specifier: public
Method name: getY
Parameters: None
Return type: int

Step 2: Instance variables

Your instance variables exist to support your methods. They serve no other purpose. Make a deliberate decision about what you need to store to be able to write your methods. It is common for students to choose inappropriate instance variables. Inappropriate instance variables can make the implementation more challenging or impossible.

Although it is possible for the instance variables to match the constructor parameters, this is not always the case. Do not just copy the constructor parameters as your instance variables unless that is actually what you want to store.

If you decide to store a list as an instance variable, take a moment to ensure that you actually need the list. It is common for students to unnecessarily store a list of values when storing information about those values would have made the problem easier.


private int x, y;

Step 3: Code inside methods

For many AP CS A Free Response that request an entire class, the implementation code for the methods themselves is simple. As with any other method implementation, make sure you understand what the method is doing, determine your algorithm, then implement the algorithm in code.

AP CS A Free Response that request an entire class often include something related to method implementations that is easily missed. Maybe there can be 0 inputs. Maybe something needs to be computed as a double. Pay attention to all of the examples, just as you would for any free response.


public void setX(int newX) // method header (part of Step 1)
    x = newX; // code inside method

When responding on paper

AP CS A students taking the Exam on paper should still follow the order above. Since it is impossible to insert line breaks on paper, use the simple trick below.

  1. Write the class header on your real answer page.
  2. Write the public method headers on a piece of scratch paper.
  3. Write the instance variables on your real answer page, under the class header as usual.
  4. Copy the first method header to your real answer page. Write the implementation for that method under the method as usual. Repeat this for each method.

Practice writing classes with AP CS Tutor Brandon Horn.

Selected AP CS A FR that request an entire class

Additional resources

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Class writing order