[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]

Constructor in Java

[/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]

What is a constructor in Java?

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, 

  • A method that instantiates objects from a class
  • We use it to assign specific attributes to an object.
  • It has the same name as the class it belongs to.
  • The constructor returns no values.
  • Available in all classes by default, but needed if you want to give specific attributes to an object.

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

How to use a constructor in Java?

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: StandardNo-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]

  • Default: If you don’t create any constructor in your class, the compiler will insert a so-called “standard constructor” in your code for you. There is nothing you see in your source code (java file) because it automatically entered into the code during compilation. However, if you implement a constructor yourself, you will no longer get a standard constructor from the compiler.
  • No-arguments: A constructor that has no input arguments, as the name suggests. The no-argument constructor is similar to the standard constructor, but the no-argument constructor can have any code, unlike the default constructor, which is empty.
  • Parameterized: Constructor that is having one or more input parameters. A parameterized constructor is used to assign different values to different objects.

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Syntax for declaring a constructor in Java

 

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

 

  • The name of the constructor must equal the name of the actual class
  • The parameters (names1 and names2) are typically the same as the class variables you define when the class is created.
  • The “this” command always refers to the current object. This means that by writing “this“, the class variable is assigned the value from the constructor. This value therefore becomes the unique value of the object.

 

 

 The constructor in Java is used to create objects from a class

[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

Example – Declaring a Constructor

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 height=”20px”][vc_column_text]Note the difference between constructors and methods in Java. Constructors are used to creating objects, and we use methods to perform operations. In the next section, we will look at how to use the constructor to create an object.[/vc_column_text][vc_empty_space height=”36px”][vc_separator type=”normal”][vc_empty_space height=”20px”][/vc_column][/vc_row][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]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]