4 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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.
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.
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.
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.
Term 1 of 12
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.
Term 1 of 12
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.
Term 1 of 12
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.
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.
Constructor: A constructor is a special method within a class that is used to initialize objects when they are created using the new keyword.
Instance Variable: An instance variable is a variable declared within a class but outside any method. Each instance/object of that class has its own copy of these variables.
Reference Variable: A reference variable holds the memory address/reference of an object rather than the actual object itself. It allows us to access and manipulate objects indirectly.
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.
Method Overloading: Method overloading is when multiple methods have the same name but different parameter lists. This allows for flexibility in calling methods with different argument combinations.
Return Type: The return type of a method specifies the type of value that the method will return after execution. It can be void if no value is returned.
Argument: An argument is a specific value or expression passed into a method or function during its invocation/call. Arguments correspond to parameters defined in the method's signature.
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.
Arguments: Arguments are actual values passed into a method when it is called. They correspond to the parameters defined in the method's declaration.
Return type: The return type specifies the type of value that a method will return after its execution. Parameters help determine what inputs are needed for the method, while return types define what output can be expected.
Method signature: A method signature consists of its name and parameter list. It uniquely identifies a specific method within a class and helps differentiate it from other methods with similar names but different parameters.
A primitive value is a basic data type in programming that represents simple pieces of information such as numbers or characters.
Non-primitive value: Non-primitive values are more complex data types that can hold multiple values or have built-in methods.
Integer: An integer is a type of primitive value that represents whole numbers without any decimal places.
Boolean: A boolean is a type of primitive value that represents either true or false.
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.
Signature: A signature refers to the unique combination of a method's name and parameter types. It helps distinguish between different methods with similar names but different parameters.
Abstraction: Abstraction is the process of simplifying complex systems by focusing on essential details while hiding unnecessary complexities. It allows programmers to work with high-level concepts without worrying about implementation details.
Object: An object is an instance of a class in object-oriented programming. It contains both data (attributes) and behavior (methods), allowing us to represent real-world entities or abstract concepts in code.
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.
NullPointerException: This is an exception that occurs when you try to use/access an object reference that points to null (no object). It's like trying to use an empty box without putting anything inside.
IndexOutOfBoundsException: This exception occurs when you try to access elements outside the valid range of indexes in arrays or collections. It's similar to trying to reach for something beyond the boundaries of your reach.
ArrayIndexOutOfBoundsException: This is a specific type of IndexOutOfBoundsException that occurs when you try to access an array element with an index that is outside the valid range of indexes for that array.
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".
Default constructor: A default constructor is a special method in Java that creates an object of a class with no arguments. It is automatically provided by Java if no other constructors are defined.
Null objects: Null objects are placeholders or dummy objects that have the same interface as real objects but do not perform any meaningful operations. They can be used to avoid NullPointerExceptions by providing default behavior when an actual object is expected but not available.
Object initialization: Object initialization refers to the process of creating and assigning initial values or references to variables within an object. It ensures that all necessary resources are properly allocated before using them in the program.