← Back to Core Java Course | Chapter 7: OOP Core | Lesson 3 of 11

Java के Constructors

एक constructor वह setup step है जो एक नया object बनने पर चलता है, एक नए student के लिए एक name tag भरने जैसा। यह object को इसकी पहली values देता है।
Syntax
java
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

java
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);
	}
}

Parameterized Constructors

Parameterized constructors arguments accept करते हैं ताकि caller एक object की initial field values को बिल्कुल creation time पर set कर सके, जैसे new Point(3, 4), object बनाने और फिर बाद में हर field अलग से set करने के बजाय।

उदाहरण: Parameterized Constructors

java
// 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);
	}
}

Constructor Overloading

Constructor overloading एक class को अलग parameter lists वाले कई constructors define करने देता है, callers को एक object बनाने के कई valid तरीके देते हुए — उदाहरण के लिए, एक constructor जो पूरी details लेता है और दूसरा जो बाकी के लिए sensible defaults भरता है।

उदाहरण: Constructor Overloading

java
// 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);
	}
}

The Copy Constructor

एक copy constructor उसी class का एक मौजूदा object लेता है और एक नया, independent object initialize करने के लिए इसकी field values उपयोग करता है — तब उपयोगी जब आप उसी underlying object को reference करने वाले दो variables के बजाय एक genuine duplicate चाहते हैं।

उदाहरण: The Copy Constructor

java
// 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);
	}
}

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()

java
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक constructor में एक return type जोड़ना, जैसे public void Person(), जो इसे एक normal method बनाता है।
  2. एक parameterized constructor define करना और फिर new Person() call करना, जो fail होता है क्योंकि default वाला अब नहीं बनता।
  3. this() या super() को पहले statement के रूप में call न करना, जो एक compile error है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.