Constructor in JAVA

 

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

PROGRAM:-

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.

 

PROGRAM:-

class Student

{

int roll;

String name;




Student (Student k)

{

roll=k.roll;

name=k.name;

}

Student()

{




}

public static void main(String[] args)

{

Student s1=new Student();

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

s1.roll=100;

s1.name="abc";

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

Student s2=new Student(s1);

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

s2.roll=101;

s2.name="abcd";

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

Student s3=new Student(s2);

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

s3.name="abcde";

s3.roll=102;

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

Student s4=new Student(s3);

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

}

}




O/P

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
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community

31 thoughts on “Constructor in JAVA

  • July 20, 2018 at 6:16 pm
    Permalink

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

    Reply
  • August 15, 2018 at 7:52 pm
    Permalink

    Thanks so much for the article post.Really looking forward to read more.

    Reply
  • August 19, 2018 at 1:29 pm
    Permalink

    Appreciate you sharing, great blog post.Really looking forward to read more. Want more.

    Reply
    • basicittopic
      August 21, 2018 at 3:50 pm
      Permalink

      Thanks for giving the feedback. yes, I definitely publish other posts.

      Reply
  • August 25, 2018 at 9:26 am
    Permalink

    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.

    Reply
  • August 26, 2018 at 1:32 am
    Permalink

    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.

    Reply
  • August 31, 2018 at 11:46 am
    Permalink

    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.

    Reply

Leave a Reply

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