The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

πŸ“±

Unit 2

2.2 Creating and Storing Objects (Initialization)

4 min readβ€’september 17, 2020

Peter Cao


Constructors and Initialization

Now that we know what an object is, how do we make an object? To make an object, we initialize it by calling its constructor. A constructor initializes the object and sets up its characteristics. Let's make an example. A student has a name, age, and a grade level, which will be initialized with the person constructor, as such:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2FAPCSA%202-Qnn64YvqwIrP.jpg?alt=media&token=2b0ce3ed-0fc9-4d35-b92c-7b7b7028db70
The Person is the name of the class, which usually has its first letter capitalized according to Java naming conventions. By using the new keyword, we are calling the constructor to make a new Person. Inside the parentheses is the parameter list, where the values of the object's characteristics are entered. The parameters are the values passed, in this case, "Peter", 17, and 13. Object names follow the same rules as variable names (because they can also be used as variables), and also follow camel-case naming conventions, where the first letter is lowercase while the first letter of any other words are uppercase.
Java is a pass-by-value language. This means that when we pass a primitive value, like an integer, we are passing a copy of that value. So if we change the value inside a constructor, the value outside the constructor doesn't change. However, when we are passing a reference type, we are passing a reference to the data slot. If we modify the data of an object in a constructor, for example, the object changes outside the constructor as well.
The Person class is located in the file "Person.java," where the constructor is found. We will not be discussing the implementation of a constructor in this unit, but only the signature of a constructor. The signature of the parameter is the class name plus the parameter list. However, in a signature, the parameter list solely consists of variable names and their variable types. Here is the signature of the Person constructor that is called above:
Person(String name, int age, int grade)
By calling a constructor without knowing how it works, we are practicing abstraction, one of the central principles of object-orientated programming. For example, I have made the person class and you are using that class. You don't need to know how the constructors or methods work, but just trust that it works properly.

Constructor Overloading

A class can have multiple constructors. However, to have multiple constructors, either the number of parameters must be different or the order of the variable types (not names) must be different. Each different constructor will still create an object with the same types of characteristics, but they are just created differently. This is called overloading a constructor.
For example, with the same number of parameters, we can have the following constructor be overloaded as follows:
Person(String name, int age, int grade) Person(int age, int grade, String name)
As a result, calling Person peter = new Person("Peter", 17, 13); calls the first constructor due to the order of the variable types, but calling Person peter = new Person(17, 13, "Peter"); calls the second constructor. However, calling Person peter = new Person(17, "Peter", 13); is illegal and will result in an IllegalArgumentException being thrown because there is no constructor with the parameter type order of (int, String, int).
In this case, the following two are illegal:
Person(String name, int age, int grade) Person(String name, int grade, int age)
This is because if you call Person peter = new Person("Peter", 17, 13);, we don't know if 17 is the age or grade, or whether 13 is the grade or age since there are 2 constructors with the parameter type order of (String, int, int).
A constructor can have fewer parameters as well when overloading. For example, the following are legal:
Person(String name, int age, int grade) Person(int age, int grade) Person(int age) Person()
For every parameter that is missing from the full constructor in the overloaded ones, the default parameter is given, which is a value predetermined by the maker of the class. If there are no parameters in the parameter list, then this is the default constructor, where all of the parameters are set to default values.

Null Objects

An object can also be set as follows:
Person invisible = null;
Null basically states that no object exists, and it is not stored in memory. You cannot call methods on an object that is declared as null since null objects do not have any information or characteristics set to that object. This will create a NullPointerException.

Was this guide helpful?

πŸ’ͺ🏽 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
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 3: Boolean Expressions and if Statements
πŸ•Ή
Unit 4: Iteration
βš™οΈ
Unit 5: Writing Classes
⌚️
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