If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Constructor special methods होते हैं जो कुछ भी return नहीं करते हैं। मतलब constructor without return statement define किये जाते हैं। जब किसी class का Object बनाते हैं तो Constructor automatically run होता है। इसे run करने के लिए call करने जरूरत नहीं पड़ती है। लेकिन अगर Class का Object नहीं बना तो Constructor method run नहीं होगा।
हालाँकि किसी भी class के लिए constructor define करना optional होता है। आप अपनी need के according define कर सकते हैं। हाँ , अगर किसी class में constructor नहीं होता है तो java compiler automatically empty body के साथ एक constructor create करता है।
java में constructor को class name के साथ ही define किया जाता है , जो class का name होगा वही constructor का name रखना पड़ेगा। हाँ इसमें define किये गए parameters को need के according कम या ज्यादा कर सकते हैं।
File : A.java
public class A {
// define default constructor.
A() {
System.out.println("Default Constructor");
}
public static void main(String[] args) {
// now just create an Object.
A a = new A();
}
}
javac A.java
java A
Default Constructor
हाँ , constructor define करते समय आप access modifiers का use कर सकते हैं , मतलब constructor को आप private , protected, public और default define कर सकते हैं। ऊपर जो example दिया गया है वो default constructor का example ही है।
जैसा कि आपने ऊपर पढ़ा कि constructor में आप parameters भी define कर सकते हैं। इस case में फिर आपको class object बनाते समय class name में वो parameters pass करने पडेगे।
File : A.java
public class A {
// define parameterized constructor.
A(String name, int age) {
System.out.println("Name : "+ name);
System.out.println("Age : "+ age);
}
public static void main(String[] args) {
// now just create an Object and pass arguments.
A a = new A("Rahul", 25);
// you can pass diff values for diff objects
A a2 = new A("Anuj", 60);
}
}
javac A.java
java A
Name : Rahul
Age : 25
Name : Anuj
Age : 60
जैसा कि आपने पढ़ा कि constructor without return type के method की तरह ही है। तो java methods की तरह ही आप constructors overloading कर सकते हैं।
किसी भी java class में एक से ज्यादा constructors हो सकते हैं। किसी class में एक से ज्यादा constructors को different - different parameters के साथ define करना ही different overloading कहलाता है। code execute करते समय compiler pass किये गए arguments से decide करता है कि कौन सा compiler call करना है।
File : A.java
public class A {
// ddefault constructor with no parameters.
A() {
System.out.println("Default Constructor");
}
// parameterized constructor
A(String name) {
System.out.println("Name : "+ name);
}
// another parameterized constructor
A(String name, int age) {
System.out.println("Name : "+ name + ", Age : "+ age);
}
public static void main(String[] args) {
// create object without argument.
A a1 = new A();
// now pass value.
A a2 = new A("Rahul");
A a3 = new A("Anuj", 60);
}
}
javac A.java
java A
Default Constructor
Name : Rahul
Name : Anuj, Age : 60
I Hope, अब आप Java में constructor के बारे में अच्छे से समझ गए होंगे।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook