इस ट्यूटोरियल में, हम जावा कंस्ट्रक्टरों, उनके प्रकारों और उदाहरणों की मदद से उनका उपयोग कैसे करें, इसके बारे में जानेंगे।
एक कंस्ट्रक्टर क्या है?
जावा में एक कंस्ट्रक्टर एक विधि के समान होता है जो तब बनाया जाता है जब कक्षा का एक ऑब्जेक्ट बनाया जाता है।
जावा विधियों के विपरीत, एक निर्माणकर्ता का वर्ग के समान नाम होता है और उसका कोई रिटर्न प्रकार नहीं होता है। उदाहरण के लिए,
कक्षा टेस्ट (टेस्ट) (// कंस्ट्रक्टर बॉडी)
यहाँ, Test()
एक कंस्ट्रक्टर है। इसका नाम कक्षा के समान है और इसमें रिटर्न प्रकार नहीं है।
अनुशंसित पढ़ना: निर्माता मान क्यों नहीं लौटाते हैं
उदाहरण 1: जावा कंस्ट्रक्टर
class Main ( private String name; // constructor Main() ( System.out.println("Constructor Called:"); name = "Programiz"; ) public static void main(String() args) ( // constructor is invoked while // creating an object of the Main class Main obj = new Main(); System.out.println("The name is " + obj.name); ) )
आउटपुट :
कंस्ट्रक्टर कहलाता है: नाम है Programiz
उपरोक्त उदाहरण में, हमने एक कंस्ट्रक्टर बनाया है जिसका नाम है Main()
। कंस्ट्रक्टर के अंदर, हम नाम चर के मूल्य को शुरू कर रहे हैं।
मुख्य वर्ग की एक वस्तु बनाने के विवरण पर ध्यान दें।
Main obj = new Main();
यहां, जब ऑब्जेक्ट बनाया जाता है, तो Main()
कंस्ट्रक्टर को बुलाया जाता है। और, नाम चर का मूल्य आरम्भिक है।
इसलिए, प्रोग्राम नाम चर के मान को प्रिंट करता है Programiz
।
कंस्ट्रक्टर के प्रकार
जावा में, कंस्ट्रक्टरों को 3 प्रकारों में विभाजित किया जा सकता है:
- नो-आर्ग कंस्ट्रक्टर
- परिमापक संवाहक
- डिफ़ॉल्ट निर्माता
1. जावा नो-आर्ग कंस्ट्रक्टर्स
तरीकों के समान, एक जावा कंस्ट्रक्टर का कोई पैरामीटर (तर्क) हो सकता है या नहीं भी हो सकता है।
यदि कोई कंस्ट्रक्टर किसी पैरामीटर को स्वीकार नहीं करता है, तो इसे नो-लॉजिक कंस्ट्रक्टर के रूप में जाना जाता है। उदाहरण के लिए,
private Constructor() ( // body of the constructor )
उदाहरण 2: जावा निजी नो-आर्ग कंस्ट्रक्टर
class Main ( int i; // constructor with no parameter private Main() ( i = 5; System.out.println("Constructor is called"); ) public static void main(String() args) ( // calling the constructor without any parameter Main obj = new Main(); System.out.println("Value of i: " + obj.i); ) )
आउटपुट :
कंस्ट्रक्टर को I: 5 का मान कहा जाता है
उपरोक्त उदाहरण में, हमने एक कंस्ट्रक्टर बनाया है Main()
। यहां, निर्माता किसी भी पैरामीटर को स्वीकार नहीं करता है। इसलिए, इसे नो-आर्ग कंस्ट्रक्टर के रूप में जाना जाता है।
गौर करें कि हमने कंस्ट्रक्टर को निजी घोषित किया है।
एक बार कंस्ट्रक्टर घोषित होने के बाद private
, इसे कक्षा के बाहर से एक्सेस नहीं किया जा सकता है। इसलिए, निजी निर्माणकर्ता का उपयोग करके कक्षा के बाहर से वस्तुओं को बनाना निषिद्ध है।
यहां, हम उसी वर्ग के अंदर ऑब्जेक्ट बना रहे हैं। इसलिए, प्रोग्राम कंस्ट्रक्टर तक पहुंचने में सक्षम है। अधिक जानने के लिए, जावा इंप्लीमेंट प्राइवेट कंस्ट्रक्टर पर जाएं।
हालांकि, यदि हम कक्षा के बाहर वस्तुओं को बनाना चाहते हैं, तो हमें कंस्ट्रक्टर को घोषित करने की आवश्यकता है public
।
उदाहरण 3: जावा पब्लिक नो-आर्ग कंस्ट्रक्टर
class Company ( String name; // public constructor public Company() ( name = "Programiz"; ) ) public class Main ( public static void main(String() args) ( // object is created in another class Company obj = new Company(); System.out.println("Company name = " + obj.name); ) )
आउटपुट :
कंपनी का नाम = प्रोग्रामिज़
अनुशंसित पढ़ना: जावा एक्सेस संशोधक
2. जावा पैरामीकृत कंस्ट्रक्टर
एक जावा निर्माता एक या अधिक मापदंडों को भी स्वीकार कर सकता है। ऐसे कंस्ट्रक्टरों को पैरामीटराइज्ड कंस्ट्रक्टर (मापदंडों के साथ कंस्ट्रक्टर) के रूप में जाना जाता है।
उदाहरण 4: परिमापित निर्माण
class Main ( String languages; // constructor accepting single value Main(String lang) ( languages = lang; System.out.println(languages + " Programming Language"); ) public static void main(String() args) ( // call constructor by passing a single value Main obj1 = new Main("Java"); Main obj2 = new Main("Python"); Main obj3 = new Main("C"); ) )
आउटपुट :
जावा प्रोग्रामिंग लैंग्वेज पायथन प्रोग्रामिंग लैंग्वेज सी प्रोग्रामिंग लैंग्वेज
उपरोक्त उदाहरण में, हमने एक कंस्ट्रक्टर बनाया है जिसका नाम है Main()
। यहां, निर्माता एकल पैरामीटर लेता है। अभिव्यक्ति पर ध्यान दें,
Main obj1 = new Main("Java");
Here, we are passing the single value to the constructor. Based on the argument passed, the language variable is initialized inside the constructor.
3. Java Default Constructor
If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor.
Example 5: Default Constructor
class Main ( int a; boolean b; public static void main(String() args) ( // A default constructor is called Main obj = new Main(); System.out.println("Default Value:"); System.out.println("a = " + obj.a); System.out.println("b = " + obj.b); ) )
Output:
a = 0 b = false
Here, we haven't created any constructors. Hence, the Java compiler automatically creates the default constructor.
The default constructor initializes any uninitialized instance variables with default values.
Type | Default Value |
---|---|
boolean | false |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
char | u0000 |
float | 0.0f |
double | 0.0d |
object | Reference null |
In the above program, the variables a and b are initialized with default value 0 and false
respectively.
The above program is equivalent to:
class Main ( int a; boolean b; // a private constructor private Main() ( a = 0; b = false; ) public static void main(String() args) ( // call the constructor Main obj = new Main(); System.out.println("Default Value:"); System.out.println("a = " + obj.a); System.out.println("b = " + obj.b); ) )
The output of the program is the same as Example 5.
Important Notes on Java Constructors
- Constructors are invoked implicitly when you instantiate objects.
- The two rules for creating a constructor are:
The name of the constructor should be the same as the class.
A Java constructor must not have a return type. - If a class doesn't have a constructor, the Java compiler automatically creates a default constructor during run-time. The default constructor initializes instance variables with default values. For example, the
int
variable will be initialized to0
- Constructor types:
No-Arg Constructor - a constructor that does not accept any arguments
Parameterized constructor - a constructor that accepts arguments
Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined. - A constructor cannot be
abstract
orstatic
orfinal
. - A constructor can be overloaded but can not be overridden.
Constructors Overloading in Java
Similar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructors overloading.
Example 6: Java Constructor Overloading
class Main ( String language; // constructor with no parameter Main() ( this.language = "Java"; ) // constructor with a single parameter Main(String language) ( this.language = language; ) public void getName() ( System.out.println("Programming Langauage: " + this.language); ) public static void main(String() args) ( // call constructor with no parameter Main obj1 = new Main(); // call constructor with a single parameter Main obj2 = new Main("Python"); obj1.getName(); obj2.getName(); ) )
Output:
Programming Language: Java Programming Language: Python
उपरोक्त उदाहरण में, हमारे पास दो निर्माता हैं: Main()
और Main(String language)
। यहां, दोनों कंस्ट्रक्टर अलग-अलग मानों के साथ चर भाषा के मूल्य को इनिशियलाइज़ करते हैं।
ऑब्जेक्ट निर्माण के दौरान पारित पैरामीटर के आधार पर, अलग-अलग निर्माणकर्ताओं को बुलाया जाता है और विभिन्न मान असाइन किए जाते हैं।
एक कंस्ट्रक्टर को दूसरे कंस्ट्रक्टर से कॉल करना भी संभव है। अधिक जानने के लिए, जावा कॉल वन कंस्ट्रक्टर को दूसरे से देखें।
नोट : हमने this
कक्षा के चर को निर्दिष्ट करने के लिए कीवर्ड का उपयोग किया है । this
कीवर्ड के बारे में अधिक जानने के लिए , इस कीवर्ड पर जाएं।