Constructor in JAVA-hindi-vishmy1.blogspot.com

Constructor in JAVA

Constructor  are a special kind of a method, they have three character-sticks.

कंस्ट्रक्टर(Constructor) एक विशेष प्रकार की विधि(method) है, उनके पास तीन चरित्र-छड़ें(character-sticks) हैं।

1 )- Constructor must have the same name as the class name itself.

कन्स्ट्रक्टर का वही नाम होना चाहिए जो क्लास के नाम का है।

2 )- Constructor doesn't have a return type not even void.

कंस्ट्रक्टर के पास वापसी(return type) का प्रकार नहीं है, यहां तक कि शून्य(void) भी नहीं।

3 )- Constructor are invoke using the new operation when the object is creating.

ऑब्जेक्ट बनाते समय कंस्ट्रक्टर को नए ऑपरेशन(new operation) का उपयोग करके लगाया जाता है।

Constructor plays the role of initializing object like regular method, Constructor can be overloaded.

कन्स्ट्रक्टर नियमित विधि की तरह ऑब्जेक्ट को इनिशियलाइज़ करने की भूमिका निभाता है, कंस्ट्रक्टर को ओवरलोड किया जा सकता है।

Syntax :-

class class_name
{
     statement;
     class_name();
     {
         statement;
     }
}

There are two types of constructor :-



Constructor in JAVA-hindi-vishmy1.blogspot.com
Constructor in JAVA


1 )- default constructor.

2 )- Constructor with parameter.

3 )- Non-parameterized constructor.


1 )- default constructor :- 

If you don't implement any constructor in your class, java compiler inserts a default constructor into your code on your behalf. This constructor is known as default constructor. when you write the code & you don't write the constructor in (java file) then it will write automatically in your code during compilation in (.class file). process of adding constructor after compilation shown below.

यदि आप अपनी कक्षा(class) में किसी भी कंस्ट्रक्टर(constructor) को लागू नहीं करते हैं, तो जावा कंपाइलर(compiler) आपकी ओर से आपके कोड में एक डिफ़ॉल्ट कंस्ट्रक्टर सम्मिलित(inserted) करता है। इस कंस्ट्रक्टर को डिफॉल्ट कंस्ट्रक्टर(default constructor) के रूप में जाना जाता है। जब आप कोड लिखते हैं और आप कंस्ट्रक्टर को (जावा फ़ाइल में) नहीं लिखते हैं तो यह आपके कोड में स्वचालित रूप से (.class फ़ाइल में) संकलन(compilation ) के दौरान लिखेगा। नीचे दिखाए गए संकलन के बाद कंस्ट्रक्टर को जोड़ने की प्रक्रिया।



Constructor in JAVA-hindi-vishmy1.blogspot.com
  
Note :- If you have added constructor in your JAVA file, then the compiler won't add constructor by its own

यदि आपने अपनी JAVA फ़ाइल में कंस्ट्रक्टर को जोड़ दिया है, तो कंपाइलर कंस्ट्रक्टर को अपने आप नहीं जोड़ेगा।

2 )- Constructor with parameter :-

Constructor with parameter or argument is known as constructor with parameter or Parameterized Constructor.

पैरामीटर या तर्क(argument) के साथ कंस्ट्रक्टर को पैरामीटर या पैरामीटरेटिड कंस्ट्रक्टर के साथ कंस्ट्रक्टर के रूप में जाना जाता है।

ex :- 

class student
{
    student(String name, int age)//parameterized constructor
    {
         System.out.println("Your name is :"+name);
         System.out.println("your age is :"+age);
     }
     public static void main(String args[])
     {
           student s = new student("vishmyblog", 21);
      }
}


3 )- Non-parameterized constructor :-

Constructor with no parameter or argument is known as Non-parameterized constructor. In this we don't pass the argument.

बिना पैरामीटर या तर्क के साथ कंस्ट्रक्टर को गैर-पैरामीटर किए गए निर्माता के रूप में जाना जाता है। इसमें हम तर्क पास नहीं करते हैं।

ex :- 

class student
{
    String name; 
    int age;
    student()//Non-parameterized constructor
    {
         System.out.println("Your name is :"+name);
         System.out.println("your age is :"+age);
     }
     public static void main(String args[])
     {
           student s = new student("vishmyblog", 21);
      }
}



Note :-  Constructor is invoked automatically when the object is created, there is no need to call constructor.

जब ऑब्जेक्ट बनाया जाता है तो कंस्ट्रक्टर को स्वचालित रूप से आमंत्रित किया जाता है, कंस्ट्रक्टर को कॉल करने की आवश्यकता नहीं होती है।


If you like this then please subscribe and follow for more and send it to your friend also.
                                   
                                                                                Thank You





Post a Comment

0 Comments