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
-
Copy constructor
- If the constructor is responsible to copy the contents of one object to another then this is known as copy constructor.
-
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
31 replies on “Constructor in JAVA”
I conceive this web site has got some real good information for everyone
Thank U
Hello. impressive job. I did not anticipate this. This is a impressive story. Thanks!
Thank U So much… also read another article and give feedback?
good stuff. I will make sure to bookmark your blog.
Thank U
Don’t wear seat belts lest you drown in you own urine? With thanks! Valuable information!
Thnq for giving the feedback
here! Good luck for the next!
Thanks for giving the feedback
“Muchos Gracias for your blog. Really thank you! Keep writing.”
Thanks for giving the feedback
Thanks so much for the article post.Really looking forward to read more.
thanks for giving the feedback
Hey, thanks for the post. Great.
Thanks for giving the feedback
Thanx for giving the feedback
Thank you ever so for you blog post.Much thanks again. Fantastic.
Thanks
I think this is a real great blog. Keep writing.
Thanx for giving the feedback
Appreciate you sharing, great blog post.Really looking forward to read more. Want more.
Thanks for giving the feedback
Im grateful for the article post.Really looking forward to read more. Want more.
Thanks for giving the feedback. yes, I definitely publish other posts.
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.
Thanks for giving the feedback.??
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.
Thank u ??
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.
Thank u so much for giving the feedback .