zuai-logo
zuai-logo
  1. AP Computer Science A
FlashcardFlashcardStudy GuideStudy Guide
Question BankQuestion Bank

Writing Methods

Ethan Taylor

Ethan Taylor

5 min read

Next Topic - Static Variables and Methods
Study Guide Overview

This study guide covers pass-by-value for primitive and reference variables in Java. It focuses on writing methods in Java classes, including examples for an Assignment and Student class. The guide demonstrates method structure with Javadoc comments, new instance variables, and explains method headers (access modifiers, return types, method names, and parameters).

Now, it’s time to write most of the rest of our methods. But first, a quick reminder about pass-by-value from Unit 2. When we put a primitive variable in as a parameter, we are giving the method a copy of that value so if you change the value of that variable, it does not get carried over to outside that method.

Meanwhile, if you put a reference variable or object as a parameter, it passes a copy of the reference to the method. Changing the value of the variable has the same effect as a primitive variable, but changing information about the variable (such as using a getter method) will carry over outside the method as well. Normally we do not want to modify mutable objects that are passed in as parameters.

#Writing Methods

Now, we will write some of the methods for our two classes (others will require information from future topics). Take note of the Javadoc comments and also new instance variables that we have added in order for these methods to work. Any changes we have made to the class will be bolded as in previous topics.

#Java Example

/** Represents an...
Feedback stars icon

How are we doing?

Give us your feedback and let us know how we can improve

Previous Topic - Mutator MethodsNext Topic - Static Variables and Methods

Question 1 of 11

What happens when you pass a primitive variable to a method in Java? 🤔

The method receives the original variable itself

The method receives a copy of the variable's value

The method receives a reference to the variable's memory location

The method cannot access primitive variables