Constructor In Java Full Details In Hindi - Create Rules
constructor को object बनाने केलिए use किया जाता है and value को initialization करने केलिए भी use किया जाता है .value को initialization करने केलिए आप setter method को use कर सकते हो नही तो constructor को use कर सकते हो .
Rules For constructor
constructor name and class name same होना चाहिए .
ex-class Employee{
Employee(){ //constructor
}
}
हम constructor में parameters देकर लेख सकते हैं
जब object create होता तब constructor execute होता है .
constructor 4 modifiers allow करता है public , private ,default and protected .
non access modifiers constructor में allow नही है .
constructor में return type नही रहेता है .
एक program में at least one constructor होता है .यदि programmer कुछ भी constructor नही create करता है तो by default compile time पे एक constructor generate होता है .
यदि आप constructor में कुछ भी return type लगा देते हैं तो आपको error नही आएगा कियुनकी वह एक method हो गया .
constructor दो प्रकार की है एक है default constructor and user defined constructor .
default constructor में हर time 0 argument रहेता है ,लिकिन user defined constructor में या तो 0 argument रहेता है और perameterized रहेता है .
public class Employee{
Employee(){
System.out.println("Java in Hindi");
}
public static void main(String[]args){
Employee s1=new Employee ();
Employee s2=new Employee ();
}
}
Output-
Java in Hindi
Java in Hindi
इसका जो result है वह दो बार प्रिंट हुआ कियुनकी रूल यह है की जब object create होता है तब constructor execute होता है .इसीलिए यह दो बार प्रिंट हुआ है .
Private Constructor-
ऊपर में आपको बताया की constructor only access modifier को allow करता है (public , private ,default and protected) and non access modifiers को allow नही करता है .
(Constructor In Java Full Details In Hindi - Create Rules)
आपको कुछ भी problem ही java से जुडी जानकारी से तो आप हमें comment section में comment कर सकते हैं .में अभी एक software developer हूँ मुझे पता है आपको कैसा पढना है .यह था आज का टॉपिक reserved keywords के बारे में .उम्मीद करता हूँ आप लोगो को यह आर्टिकल पसंद आया होगा यदि आया है .
तो आपके दोस्तों के साथ शेयर करें .
More
0 Comments