Constructor Function in Java - ProgrammerTech
Language:

Search

Constructor Function in Java

  • Share this:
Constructor Function in Java

Constructor in Java

The Constructor function makes it easy for us to create objects from a specific class, and it is an important thing to think about after creating the class. It is a function that has a special type, this function is called during the creation of the object in order to generate the initial values ​​of the properties in it. It is a function with the same name as the class but does not return a value and is executed automatically once the object is created.

The general form of the constructor function in java

public class Con1
Con1()
{
/Setting what we want to execute when the program is running
}

We note that a constructor function with the same name as the class Con1 will be executed automatically as soon as an object is created, and here the important point is that it is executed automatically as soon as an object is created. It means if we want to do an operation when the program is running we use a constructor, for example we want a welcome message to appear as soon as the program is running or a specific message to the user. Or, for example, we want to give initial values ​​to some variables or zero them once the program is running.

Another example is the use of a constructor in Databases and network programming, if we want once the program is run, it is directly connected to the program's base.

Example 1

package con23;
public class Con23 {
    Con23()
    {
        System.out.println("Hello Programmer Tech");
    }
    public static void main(String[] args) {
        Con23 cs=new Con23();
    }
}

And when you run the program or execute this code, the result will be.

Hello Programmer Tech

We created a Con1 class and then we created a constructor and it took the same name of the class because this function takes the same name as the class and we put it in printing Hello Programmer Tech as soon as we run the program. In the main program, we derive an object from the name of the class so that the constructor function is executed. If we remove the object, nothing will be printed because these are conditions for the constructor to work as soon as an object is created.

Also, in object oriented programming, more than one constructor function can be created, provided that they are different in number of parameters. I mean, for example, if we write another constructor, the compiler will intercept the function. I mean, it exists, why build it again, but if we change the number of parameters. We will be allowed to build because it differed from the first.

Example 2

package con23;
public class Con23 {
    int y;
    Con23()
    {
        System.out.println("Hello Programmer Tech");
    }
    Con23(int x)
    {
        y=x;
    }
    public static void main(String[] args) {
        Con23 cs=new Con23();
        Con23 cs1=new Con23(2);
        Con23 cs2=new Con23(3);
        System.out.println(cs1.y);
        System.out.println(cs2.y);
    }
}

Program execution

Hello Programmer Tech
2
3

We created a variable y and two building functions, the first is to print the welcome sentence, and the second we created the variable y=x, then we entered the main program and created three objects. The first is for the first function and prints the welcome sentence, the second holds the value 2 and the third holds the value 3. And we set the print command, which is to print the value of the first object cs1 and the second to print the value of the object cs2 and when executing, we did what we asked for.

Example 3

package con23;
public class Con23 {
    int y;
    double y1;
    Con23()
    {
        System.out.println("Hello Programmer Tech");
    }
    Con23(int x)
    {
        y=x;
    }
    Con23(double x)
    {
        y1=x;
    }
    pubنlic static void main(String[] args) {
        Con23 cs=new Con23();
        Con23 cs1=new Con23(2);
        Con23 cs2=new Con23(2.3);
        System.out.println(cs1.y); 
        System.out.println(cs2.y1);  
    }
}

When the program is executed, we will get the following result

Hello Programmer Tech
2
2.3

In this example, we created an int variable y and a double variable y1 and then created the Con23 function. And it is int x and its commands y=x Then we created a second function and the variable was double and it is y1=x. Then we entered the main program and created three objects, the first being cs for . The welcome message and the second cs1 loaded with the value 2 and the third cs2 loaded with the value 2.3. Then we wrote the first print command to print y and the second print command to print y1.

Example 4

package con23;
public class Con23 {
    int y;
    double y1;
    Con23()
    {
        System.out.println("Hello Programmer Tech");
    }
    Con23(int x)
    {
        y=x;
    }
    Con23(double x)
    {
        y1=x;
    }
    Con23(double x, int x1)
    {
        y1=x+x1;
    }
    public static void main(String[] args) {
        Con23 cs=new Con23();
        Con23 cs1=new Con23(2);
        Con23 cs2=new Con23(2.3,26);
        System.out.println(cs1.y); /cs1 Print
        System.out.println(cs2.y1); /cs2 Print
    }
}

The explanation of this example is identical to the explanation of the previous third example, but we put in it the process of adding y1=x+x1 and it is within the parametric function. When printed, the output will be.

Hello Programmer Tech
2
28.3

The basic rule when defining a constructor is that it must have the same name as the class and be public.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com