Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

2.2 Creating and Storing Objects (Instantiation)

4 min readdecember 30, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

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 , 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 , where the values of the object's characteristics are entered.

The 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 , 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 , 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 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 name plus the . However, in a signature, the 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 and you are using that . You don't need to know how the constructors or methods work, but just trust that it works properly.

Constructor Overloading

A can have multiple constructors. However, to have multiple constructors, either the number of 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 , 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 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 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 . If there are no in the , then this is the default constructor, where all of the 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 .

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.

Key Terms to Review (12)

Class

: A class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.

Constructor Overloading

: Constructor overloading is a feature in OOP that allows multiple constructors with different parameter lists within a single class. Each constructor can initialize an object with different sets of values.

IllegalArgumentException

: An IllegalArgumentException is an exception that occurs when a method receives an argument that is inappropriate or invalid for its intended purpose. It usually indicates that there was an error in how arguments were passed into a method.

Key Term: Object

: An object is an instance of a class that encapsulates data and behavior. It represents a real-world entity or concept in the program.

New Keyword

: The new keyword is used in Java to create an instance (object) of a class. It allocates memory for the object and initializes its attributes using the constructor method.

NullPointerException

: A NullPointerException occurs when a program tries to access or use an object that has not been initialized, meaning it is currently set to "null".

Object-oriented programming (OOP)

: Object-oriented programming is a programming paradigm that organizes code into objects, which are instances of classes. It emphasizes the use of objects to represent real-world entities and their interactions.

Parameter List

: A parameter list is a set of input parameters/arguments that are passed to a method or constructor when it is called. It specifies the type and order of the values that need to be provided for the method/constructor to execute correctly.

Parameters

: Parameters are variables declared in a method or function that receive values when the method is called. They allow data to be passed into a method, enabling it to perform actions or calculations based on those values.

Pass-by-value language

: Pass-by-value language refers to programming languages where copies of variable values are passed into functions. Any changes made to these copies do not affect the original variables.

Primitive value

: A primitive value is a basic data type in programming that represents simple pieces of information such as numbers or characters.

Reference Type

: A reference type is a data type that stores the memory address of an object rather than the actual value. It allows multiple variables to refer to the same object, and changes made to one variable will affect all other variables referencing that object.

2.2 Creating and Storing Objects (Instantiation)

4 min readdecember 30, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

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 , 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 , where the values of the object's characteristics are entered.

The 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 , 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 , 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 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 name plus the . However, in a signature, the 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 and you are using that . You don't need to know how the constructors or methods work, but just trust that it works properly.

Constructor Overloading

A can have multiple constructors. However, to have multiple constructors, either the number of 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 , 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 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 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 . If there are no in the , then this is the default constructor, where all of the 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 .

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.

Key Terms to Review (12)

Class

: A class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.

Constructor Overloading

: Constructor overloading is a feature in OOP that allows multiple constructors with different parameter lists within a single class. Each constructor can initialize an object with different sets of values.

IllegalArgumentException

: An IllegalArgumentException is an exception that occurs when a method receives an argument that is inappropriate or invalid for its intended purpose. It usually indicates that there was an error in how arguments were passed into a method.

Key Term: Object

: An object is an instance of a class that encapsulates data and behavior. It represents a real-world entity or concept in the program.

New Keyword

: The new keyword is used in Java to create an instance (object) of a class. It allocates memory for the object and initializes its attributes using the constructor method.

NullPointerException

: A NullPointerException occurs when a program tries to access or use an object that has not been initialized, meaning it is currently set to "null".

Object-oriented programming (OOP)

: Object-oriented programming is a programming paradigm that organizes code into objects, which are instances of classes. It emphasizes the use of objects to represent real-world entities and their interactions.

Parameter List

: A parameter list is a set of input parameters/arguments that are passed to a method or constructor when it is called. It specifies the type and order of the values that need to be provided for the method/constructor to execute correctly.

Parameters

: Parameters are variables declared in a method or function that receive values when the method is called. They allow data to be passed into a method, enabling it to perform actions or calculations based on those values.

Pass-by-value language

: Pass-by-value language refers to programming languages where copies of variable values are passed into functions. Any changes made to these copies do not affect the original variables.

Primitive value

: A primitive value is a basic data type in programming that represents simple pieces of information such as numbers or characters.

Reference Type

: A reference type is a data type that stores the memory address of an object rather than the actual value. It allows multiple variables to refer to the same object, and changes made to one variable will affect all other variables referencing that object.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.