[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]
[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1568929294493{background-color: #f6f6f6 !important;}”]
The constructor in Java is used to create objects from a class. This is where you assign the object the values you want the object to have.
[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]
A constructor is a method used to instantiate (create) an object from a class. When using a constructor from a class, the constructor creates an object from the corresponding class. It is with the help of the constructor that you specify the input parameters (initial values), that is, what attributes the object should have.
In Java, all classes have a constructor by default, even if you have not created one yourself. However, to be able to specify specific attributes for an object, you must first create a constructor. The constructor always has the same name as the class name that it belongs to.
Constructor in Java is,
[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]
When you want to create an object, you use the constructor – it’s called that you instantiate an object. When creating an object, the constructor performs its operations first. Therefore, the constructor is the first to be called when creating an object.
Furthermore, there are three types of constructors in Java: Standard, No-arguments, and Parameterized.[/vc_column_text][vc_empty_space height=”20px”][vc_single_image image=”20043″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][vc_empty_space height=”30px”][vc_column_text]
[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]
The syntax for a constructor in Java is:
// Classname must equal to the name of the actual class
// name1 and name2 are the parameters of the constructor of a particular data type
Classname(data-type name1, data-type name2){
// the word "this" refers to the current object's class variable
this.namn1 = namn1;
this.namn2 = namn2;
// And so on..
}
How to declare a constructor in Java
[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]
Continued from the previous example – In the following code, we have now created the constructor (highlighted rows) in the dog class:
class Dog {
// Defines the Attributes of the dog class
String name;
int age;
String breed;
String color;
// Define the constructor
Dog(String name, int age, String breed, String color){
this.name = name;
this.age = age;
this.breed = breed;
this.color = color;
}
// Defines the methods for the dog class
void bark(){
}
void eat(){
}
void sleep(){
}
}
And to be extra clear what the constructor looks like:
// Define the constructor
Dog(String name, int age, String breed, String color){
this.name = name;
this.age = age;
this.breed = breed;
this.color = color;
}
Moreover, let’s take a look at the constructor, and it’s components. If we look at the top line of the constructor, we first have the name of the constructor (Dog). Remember, the name must be the same as the class name; in other words, “Dog” in this case. Note that class names have a capital letter in their name as a practice.
Dog(String name, int age, String breed, String color){
The parameters in the constructor (String name, int age, String breed, String color) are, as you see, the same as the class variables that we defined when we created the class (lines 4-7). So what we say is; To create the dog object, four parameters called: name, age, breed, and color are required. The parameters can, of course, be called anything. Still, in order not to confuse them with any other variable, it is advisable to name the parameters to the same name as the class variables.
In the remaining four rows
this.name = name; this.age = age; this.breed = breed; this.color = color;
We assign the parameters to the class variables. By writing “this” in front of the parameters, we know that it is the class variables for this object. [/vc_column_text][vc_empty_space height=”20px”][vc_column_text]
[/vc_column_text][vc_empty_space][/vc_column][/vc_row]