Constructor In Java: A Simple But Complete Guide

Constructor In Java: A Simple But Complete Guide

#python #java-programming #Constructor #constructor-in-java

Avatar

 BY TEJAS





A constructor is a very important feature in Java. Also, a constructor is an important topic in object-oriented programming.

A constructor is a member function of any class whose name is the same as the name of the class. In simple language, if we learn, we make an object just like we make an object of a class, but its name is the same as the name of the class. The constructor does not have any return type. The constructor is used to set the initial values ​​of functions.



ADVERTISEMENT





★ Constructor name and class name should be the same.
★ There is no return type constructor.
★ Constructor call is made only when the object is created.

Java has two types of constructors.
1. NULL Constructor
2. Parameterized Constructor

                          
    import java.lang.*;
    class student {
        String name;
        int marks1, marks2, marks3, regno;

        // NULL constructor
        student() {
            name = "raju";
            regno = 12345;
            marks1 = 56;
            marks2 = 47;
            marks3 = 78;
        }

        // parameterized constructor
        student(String n, int r, int m1, int m2, int m3) {
            name = n;
            regno = r;
            marks1 = m1;
            marks2 = m2;
            marks3 = m3;
        }
        void display() {
            System.out.println(name + "\t" + regno + "\t" + marks1 + "\t" + marks2 + "\t" + marks3);
        }
    }
    class Constructor {
        public static void main(String arg[]) {
            student s1 = new student();
            student s2 = new student("john", 34266, 58, 96, 84);
            s1.display();
            s2.display();
        }
    }
                      

This is the Output Of Program

Output

Program Output

Looking at the above program, you may have got some idea of ​​what a constructor is?. What is a NULL constructor & what is a Parameterized constructor?



ADVERTISEMENT




1. NULL Constructor

In NULL Constructor we set the values ​​directly.
In the NULL constructor, you do not define any parameter in the function. Outside of Function, we define all datatypes and keywords. And then in the function, we assign values ​​to those keywords.
Your concept will be completely clear by watching the program given below.

                        
    import java.lang.*;
    class student {
        String name;
        int marks1, marks2, marks3, regno;
        // null constructor
        student() {
            name = "raju";
            regno = 12345;
            marks1 = 56;
            marks2 = 47;
            marks3 = 78;
        }
        void display() {
            System.out.println(name + "\t" + regno + "\t" + marks1 + "\t" + marks2 + "\t" + marks3);
        }
    }
    class StudentDemo {
        public static void main(String arg[]) {
            student s1 = new student();
            s1.display();
        }
    }
                    
NULL Constructor Output

Null Constructor Output



ADVERTISEMENT




2. Parameterized constructor

In a parameterized constructor we set the values ​​first and then pass those values ​​to the constructor.
In a parameterized constructor we can define any parameters in the function. In this, we can define datatype and keywords in the parenthesis of a function. And then like NULL Constructor we assign values ​​to those keywords.
You will get a crystal clear idea by watching the program given below.

                        
    import java.lang.*;
    class student {
        String name;
        int marks1, marks2, marks3, regno;
        // parameterized constructor
        student(String n, int r, int m1, int m2, int m3) {
            name = n;
            regno = r;
            marks1 = m1;
            marks2 = m2;
            marks3 = m3;
        }
        void display() {
            System.out.println(name + "\t" + regno + "\t" + marks1 + "\t" + marks2 + "\t" + marks3);
        }
    }
    class StudentDemo {
        public static void main(String arg[]) {
            student s2 = new student("john", 34266, 58, 96, 84);
            s2.display();
        }
    }
                    
Parameterized Constructor Output

Parameterized Constructor Output



ADVERTISEMENT




Conclusion

As we saw in the above article, what are the concepts of constructor in java? There are two types of NULL Constructor and Parameterized Constructor.
If you liked the article, please comment below.

Thank You





Search
Advertisement
Share With
Follow us on
Advertisement
Advertisement