Constructor in JAVA


Constructor in Java


The constructor in Java is very similar to the method. The main job of the constructor is to initialize the object. The constructor is a block of code that initializes the newly created object. A constructor is called automatically when a new instance of an object is created.
Each and every class in Java contain a constructor.
Java support two types of constructor:-default constructor, and copy constructor

Calling Procedure:-

1)new keyword

2)this keyword

3) Super keyword

Default constructor:-

The constructor without any argument is known as default constructor. The access specifier of the default constructor is same as the class access specifier. If there is no constructor defined by the user explicitly java compiler will provide the default constructor.

Parameterize Constructor:- 

The constructor which is designed by the user known as the parameterized constructor. If the user already defined a constructor then the compiler doesn’t provide any default constructor


Class Test


Test(int x)


System.out.println(“I am in parameterise constructor”);

public static void main(String[] args)


new Test(123);



O/P=I am in parameterized constructor


  1. Copy constructor

  • If the constructor is responsible to copy the contents of one object to another then this is known as copy constructor.
  1. Private Constructor

  • The constructor declared with a private keyword is known as the private constructor.
  • If the constructor is private we cannot create an object outside class.



class Student


int roll;

String name;

Student (Student k)







public static void main(String[] args)


Student s1=new Student();

System.out.println("state of s1:"+s1.roll+"\t\t";


System.out.println("state of s1:"+s1.roll+"\t\t";

Student s2=new Student(s1);

System.out.println("state of s2:"+s2.roll+"\t\t";


System.out.println("state of s2:"+s2.roll+"\t\t";

Student s3=new Student(s2);

System.out.println("state of s3:"+s3.roll+"\t\t";"abcde";


System.out.println("state of s3:"+s3.roll+"\t\t";

Student s4=new Student(s3);

System.out.println("state of s4:"+s4.roll+"\t\t";




state of s1:0           null

state of s1:100         abc

state of s2:100         abc

state of s2:101         abcd

state of s3:101         abcd

state of s3:102         abcde

state of s4:102         abcde


  1. Don’t wear seat belts lest you drown in you own urine? With thanks! Valuable information!

  2. I happen to be writing to let you understand of the incredible discovery my wife’s child went through checking your web site. She noticed a wide variety of pieces, including what it is like to have a wonderful teaching heart to get other individuals without difficulty grasp specified specialized issues. You truly surpassed her expected results. Thanks for distributing those great, safe, educational and even fun thoughts on the topic to Mary.

  3. Thanks a lot for providing individuals with an exceptionally wonderful opportunity to discover important secrets from this site. It is usually so useful and as well , packed with a great time for me and my office co-workers to search your site at the least 3 times in 7 days to learn the latest guides you have got. And of course, I’m so certainly motivated with your great methods you serve. Selected two areas in this article are certainly the most effective I’ve ever had.

  4. I wish to express my thanks to this writer just for rescuing me from such a difficulty. Because of scouting throughout the internet and finding recommendations that were not helpful, I figured my entire life was well over. Living without the approaches to the difficulties you have sorted out as a result of the site is a crucial case, as well as the ones that would have in a negative way damaged my entire career if I hadn’t encountered your blog post. Your primary capability and kindness in maneuvering every aspect was helpful. I’m not sure what I would’ve done if I hadn’t discovered such a subject like this. It’s possible to at this point look ahead to my future. Thanks a lot very much for this skilled and results-oriented guide. I will not hesitate to recommend the sites to any individual who ought to have support about this area.

Leave a comment

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