Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

5.2 Constructors

3 min readdecember 28, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

The Student Class

Let's start writing our classes by declaring our and headers for the :

Now, we need to make our for the . The will need to initialize all the above 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 parameters with initial values.

An object’s state refers to its attributes and their values at a given time; the state is defined by belonging to the object. Constructors are used to set the initial state of an object, which should include initial values for all .

We will first make the full below and then write the overloaded constructors in Topic 5.9.

When a is called, the parameters are local variables, which means that the variables are only defined inside that . Notice how the parameter names are different than the names of the . In Topic 5.9 we will learn how to keep both as the same name which is the conventional way to do it.

If one of the parameters for the is a (meaning that its state can be changed after it has already been created), the instance variable should be initialized with a copy of the object referenced in the parameter. This keeps us from accidentally changing the state of the original object.

Before we move on to the for the , if we don't include a for a , and we try to make a new object, Java will automatically create a default with default values set for the . Here is a list of them for certain data types:

Default Values by Data Type

Data TypeValue
Booleanfalse
Double0.0
Integer0
Objects/Reference TypesNull

The Assignment Class

With that, let's do the same with the .

To see how constructors are used to create instances of a , you can visit Topic 2.2!

Key Terms to Review (10)

Assignment

: An assignment is the act of giving a value to a variable in programming. It involves storing information into memory locations so it can be accessed and manipulated later.

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

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Instance Variables

: Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.

Key Term: String

: A string is a sequence of characters, such as letters, numbers, and symbols, that is used to represent text in programming. It is often enclosed in quotation marks.

Mutable Object

: A mutable object is an object whose state can be modified after it is created. In other words, you can change the values of its attributes or properties.

Object's State

: An object's state refers to the set of values stored in its instance variables at any given time during program execution. It represents the current condition or snapshot of an object's data.

Private

: In the context of programming, private refers to a visibility modifier that restricts access to certain variables or methods within a class. It means that only other members of the same class can access those private elements.

public

: Public is an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.

Student

: A student is an individual who is enrolled in a school or educational institution and is actively pursuing knowledge and education.

5.2 Constructors

3 min readdecember 28, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

The Student Class

Let's start writing our classes by declaring our and headers for the :

Now, we need to make our for the . The will need to initialize all the above 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 parameters with initial values.

An object’s state refers to its attributes and their values at a given time; the state is defined by belonging to the object. Constructors are used to set the initial state of an object, which should include initial values for all .

We will first make the full below and then write the overloaded constructors in Topic 5.9.

When a is called, the parameters are local variables, which means that the variables are only defined inside that . Notice how the parameter names are different than the names of the . In Topic 5.9 we will learn how to keep both as the same name which is the conventional way to do it.

If one of the parameters for the is a (meaning that its state can be changed after it has already been created), the instance variable should be initialized with a copy of the object referenced in the parameter. This keeps us from accidentally changing the state of the original object.

Before we move on to the for the , if we don't include a for a , and we try to make a new object, Java will automatically create a default with default values set for the . Here is a list of them for certain data types:

Default Values by Data Type

Data TypeValue
Booleanfalse
Double0.0
Integer0
Objects/Reference TypesNull

The Assignment Class

With that, let's do the same with the .

To see how constructors are used to create instances of a , you can visit Topic 2.2!

Key Terms to Review (10)

Assignment

: An assignment is the act of giving a value to a variable in programming. It involves storing information into memory locations so it can be accessed and manipulated later.

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

: A constructor is a special method within a class that is used to initialize objects of that class. It is called automatically when an object is created and helps set initial values for its attributes.

Instance Variables

: Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.

Key Term: String

: A string is a sequence of characters, such as letters, numbers, and symbols, that is used to represent text in programming. It is often enclosed in quotation marks.

Mutable Object

: A mutable object is an object whose state can be modified after it is created. In other words, you can change the values of its attributes or properties.

Object's State

: An object's state refers to the set of values stored in its instance variables at any given time during program execution. It represents the current condition or snapshot of an object's data.

Private

: In the context of programming, private refers to a visibility modifier that restricts access to certain variables or methods within a class. It means that only other members of the same class can access those private elements.

public

: Public is an access modifier in Java that indicates unrestricted visibility for classes, methods, and variables. Public members can be accessed from any other class or package.

Student

: A student is an individual who is enrolled in a school or educational institution and is actively pursuing knowledge and education.


© 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.