Default Constructor in java program

What is default constructor?

– In java program, when we set same class name and method name that is called constructor structure in java program. And when we set null argument as method it’s call default constructor.

Constructor method does not have return type.

3 types of constructor available in java program.

1) Default constructor, 2) parameter constructor, 3) copy constructor

 

In this blog we are working on default constructor programs.

 

 

Let’s see example on default constructor.

class A

{

            A()       // class name == Method name | No return type like void, int etc

            {

                        System.out.println(“Welcome in Earn online Money with Portal Isssearch”);

                        int i=1; 

                        while(i<=10)

                        {

                                    System.out.print(i + “ ”);          

                                    i++;     

                        }

                        System.out.println();

            }

}                      

class clEx9

{

            public static void main(String args[])

            {

                        // Create an object of class A

                        A a1 = new A();            // Call to A() constructor in class A

 

                        // When we create an object and call to constructor

 

                        // another process to call default constructor

                        new A();           // call to A() in class A

            }

}

 

Output:

Welcome in Earn online Money with Portal Isssearch

1 2 3 4 5 6 7 8 9 10

Welcome in Earn online Money with Portal Isssearch

1 2 3 4 5 6 7 8 9 10

 

This is very simple example of constructor. We can use two methods for call constructor method in java program.

 

Video available on YouTube.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

154total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint