πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

βš™οΈ

Unit 5

5.2 Constructors

2 min readβ€’november 16, 2020

peter-cao

Peter Cao


The Student Class

Let's start writing our classes by declaring our instance variables and class headers for the Student class:
public class Student { private int gradeLevel; private String name; private int age; private Assignment assignment; }
Now, we need to make our constructor for the student class. The constructor will need to initialize all the above instance variables and allow the client to have the opportunity to set some of the variables to values of their own choosing. For this, we are allowing gradeLevel, name, and age to be set. These will be our constructor parameters with initial values. We will first make the full constructor below and then write the overloaded constructors in Topic 5.9.
public class Student { private int gradeLevel; private String name; private int age; private Assignment assignment; public Student(int gradeLev, String fullName, int ageNum) { gradeLevel = gradeLev; name = fullName; age = ageNum; assignment = null; } }
When a constructor is called, the parameters are local variables, which means that the variables are only defined inside that constructor. Notice how the parameter names are different than the names of the instance variables, but in Topic 5.9 we will learn how to keep both as the same name which is the conventional way to do it.
Before we move on to the Assignment constructor, if we don't include a constructor for a class, and we try to make a new object, Java will automatically create a default constructor with default values set for the instance variables. Here is a list of them for certain data types:

Default Values By Data Type

The Assignment Constructor

With that, let's do the same with the Assignment class.
public class Assignment { private boolean correctAnswer; public Assignment(boolean answer) { correctAnswer = answer; } }

Was this guide helpful?

πŸ” Are you ready for college apps?
Take this quiz and find out!
Start Quiz
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
πŸ™
Exam Reviews
πŸ–±
Unit 10: Recursion
βž•
Unit 1: Primitive Types
πŸ“±
Unit 2: Using Objects
πŸ–₯
Unit 3: Boolean Expressions and if Statements
πŸ•Ή
Unit 4: Iteration
⌚️
Unit 6: Array
πŸ’Ύ
Unit 7: ArrayList
πŸ’»
Unit 8: 2D Array
πŸ–²
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741 to get started.
Β© 2021 Fiveable, Inc.