Java के Constructors
In this page:
class ClassName {
ClassName(dataType parameter) {
this.field = parameter;
}
}
ClassName object = new ClassName(argument);
The Default Constructor
एक constructor एक नए बने object की state initialize करता है, और अगर आप खुद एक नहीं लिखते, Java चुपचाप एक no-argument default constructor generate करता है जो default-initializing fields (type के आधार पर 0, false, या null) से आगे कुछ नहीं करता।
उदाहरण: The Default Constructor
class Dog {
String name = "Unnamed";
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog(); // uses Java's silently generated default constructor
System.out.println(dog.name);
}
}
Login to try C/C++/Java/PHP code in the editor
Parameterized Constructors
Parameterized constructors arguments accept करते हैं ताकि caller एक object की initial field values को बिल्कुल creation time पर set कर सके, जैसे new Point(3, 4), object बनाने और फिर बाद में हर field अलग से set करने के बजाय।
उदाहरण: Parameterized Constructors
// Define the class Point
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p
Point p = new Point(3, 4);
// Print a line to the console
System.out.println(p.x + "," + p.y);
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Overloading
Constructor overloading एक class को अलग parameter lists वाले कई constructors define करने देता है, callers को एक object बनाने के कई valid तरीके देते हुए — उदाहरण के लिए, एक constructor जो पूरी details लेता है और दूसरा जो बाकी के लिए sensible defaults भरता है।
उदाहरण: Constructor Overloading
// Define the class Point
class Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p1
Point p1 = new Point();
// Create a new Point object and store it in p2
Point p2 = new Point(3, 4);
// Print a line to the console
System.out.println(p1.x + "," + p2.x);
}
}
Login to try C/C++/Java/PHP code in the editor
The Copy Constructor
एक copy constructor उसी class का एक मौजूदा object लेता है और एक नया, independent object initialize करने के लिए इसकी field values उपयोग करता है — तब उपयोगी जब आप उसी underlying object को reference करने वाले दो variables के बजाय एक genuine duplicate चाहते हैं।
उदाहरण: The Copy Constructor
// Define the class Point
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
Point(Point other) {
this.x = other.x;
this.y = other.y;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in original
Point original = new Point(3, 4);
// Create a new Point object and store it in copy
Point copy = new Point(original);
// Print a line to the console
System.out.println(copy.x + "," + copy.y);
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Chaining with this()
एक constructor के अंदर this(...) उसी class में एक दूसरे constructor को call करता है, आपको setup logic दोहराने के बजाय कई constructors को एक central initialization path से funnel करने देते हुए।
Java चाहता है यह call constructor body में बिल्कुल पहला statement हो।
उदाहरण: Constructor Chaining with this()
// Define the class Point
class Point {
int x, y;
Point() {
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p
Point p = new Point();
// Print a line to the console
System.out.println(p.x + "," + p.y);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक constructor में एक return type जोड़ना, जैसे
public void Person(), जो इसे एक normal method बनाता है। - एक parameterized constructor define करना और फिर
new Person()call करना, जो fail होता है क्योंकि default वाला अब नहीं बनता। this()याsuper()को पहले statement के रूप में call न करना, जो एक compile error है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: