2 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Let's start writing our classes by declaring our instance variables and class headers for the Student class:
[public](https://www.fiveableKeyTerm:public) class Student {
[private](https://www.fiveableKeyTerm:Private) int gradeLevel;
private String name;
private int age;
private [Assignment](https://www.fiveableKeyTerm: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.
An object’s state refers to its attributes and their values at a given time; the state is defined by instance variables belonging to the object. Constructors are used to set the initial state of an object, which should include initial values for all instance variables.
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. 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 constructor is a mutable object (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 constructor for the Assignment class, 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:
Data Type | Value |
Boolean | false |
Double | 0.0 |
Integer | 0 |
Objects/Reference Types | Null |
With that, let's do the same with the Assignment class.
public class Assignment {
private boolean correctAnswer;
public Assignment(boolean answer) {
correctAnswer = answer;
}
}
To see how constructors are used to create instances of a class, you can visit Topic 2.2!
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.
Term 1 of 10
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.
Term 1 of 10
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.
Term 1 of 10
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.
Methods: Methods are actions or behaviors associated with objects in a class. They define what an object can do or how it can interact with other objects.
Constructor: A constructor is a special method within a class that is automatically called when an object is created from that class. It initializes the object's state by assigning initial values to its instance variables.
Access Modifiers: Access modifiers determine the accessibility or visibility of classes, methods, and variables in object-oriented programming. They control which parts of a program can access certain elements and help enforce encapsulation and data hiding principles.
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.
Object: An object is an instance of a class. It represents a specific entity with its own set of data and behavior.
Inheritance: Inheritance is the process by which one class inherits the properties and behaviors of another class. It allows for code reuse and promotes modularity.
Encapsulation: Encapsulation is the practice of hiding internal details of an object and providing access only through well-defined interfaces. It helps maintain code integrity and prevents unauthorized access.
A student is an individual who is enrolled in a school or educational institution and is actively pursuing knowledge and education.
Grade Level: The grade level refers to the specific year or level of education that a student is currently in.
Attendance: Attendance refers to the act of being present at school or in class regularly.
GPA (Grade Point Average): GPA is a numerical representation of a student's academic performance, calculated by averaging the grades they have received in their courses.
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.
Class: A class is a blueprint or template for creating objects in object-oriented programming. Constructors belong to classes and define how objects of that class should be initialized.
Instance variables: Instance variables are attributes or properties associated with each instance (object) of a class. Constructors often assign initial values to these instance variables.
Overloading: Constructor overloading allows multiple constructors within the same class, but with different parameter lists. This provides flexibility in creating objects with varying initialization options.
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.
Immutable Object: An immutable object is the opposite of a mutable object. Once an immutable object is created, its state cannot be changed.
Attribute: An attribute refers to a characteristic or property of an object. It represents the data associated with the object.
Method: A method is a set of instructions that defines what actions an object can perform. It represents the behavior of an object.