πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ“±

2.2 Creating and Storing Objects (Instantiation)

4 min readβ€’june 18, 2024

Avanish Gupta

Avanish Gupta

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

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:
Person peter = new Person("Peter", 17, 13);
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.
In Java, variables of reference types (such as classes and arrays) hold a reference to an object in memory, rather than the actual object itself. This reference is a memory address that points to the location in memory where the object is stored.
If there is no object associated with a reference variable, the value of the variable is null, which indicates that the variable does not currently reference any object.
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.