← Back to Kotlin Course | Chapter 6: Classes & Objects | Lesson 2 of 7

Constructors क्या हैं

Constructors वे खास setup instructions हैं जो एक class तब चलाती है जब भी उस kind का एक बिल्कुल नया object बनता है।
Syntax
kotlin
class ClassName(val property: Type, param: Type) {   // primary constructor
    constructor(other: Type) : this(other, defaultValue) {
        // secondary constructor
    }
}

The Primary Constructor

Primary constructor खुद class header का हिस्सा है, class name के ठीक बाद एक instance बनाने के लिए ज़रूरी parameters list करते हुए।

उदाहरण: The Primary Constructor

markup
// Define a class `Book`
class Book(val title: String, val author: String)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `book`, set to `Book("Kotlin in Action", "Dmitry Jemerov")`
    val book = Book("Kotlin in Action", "Dmitry Jemerov")
    // Print "${book.title} by ${book.author}" to the console, with a trailing newline
    println("${book.title} by ${book.author}")
}

Secondary Constructors

class body के अंदर एक constructor keyword instance बनाने का एक वैकल्पिक तरीका define करता है; हर secondary constructor को आखिरकार : this(...) का उपयोग करके primary constructor को delegate करना चाहिए।

उदाहरण: Secondary Constructors

markup
// Define a class `Book`
class Book(val title: String, val author: String) {
    constructor(title: String) : this(title, "Unknown")
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `book`, set to `Book("Kotlin Basics")`
    val book = Book("Kotlin Basics")
    // Print "${book.title} by ${book.author}" to the console, with a trailing newline
    println("${book.title} by ${book.author}")
}

Constructor Parameters vs Properties

primary constructor में बिना val/var के listed एक parameter सिर्फ constructor और किसी init blocks के अंदर उपलब्ध है -- इसे member के रूप में उजागर करने के लिए, इसे val या var declare करना चाहिए, या explicitly किसी property को assign करना चाहिए।

उदाहरण: Constructor Parameters vs Properties

markup
// Define a class `Circle`
class Circle(radius: Double) {
    // Declare a read-only variable `area` (a Double), set to `Math.PI * radius * radius`
    val area: Double = Math.PI * radius * radius
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `circle`, set to `Circle(2.0)`
    val circle = Circle(2.0)
    // Print "Area: ${circle.area}" to the console, with a trailing newline
    println("Area: ${circle.area}")
}

Default Values in Constructors

बिल्कुल regular functions की तरह, constructor parameters के default values हो सकते हैं, instance बनाते समय कुछ arguments को छोड़ा जा सकने देते हुए।

उदाहरण: Default Values in Constructors

markup
// Define a class `User`
class User(val name: String, val role: String = "member")

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `admin`, set to `User("Ana", "admin")`
    val admin = User("Ana", "admin")
    // Declare a read-only variable `user`, set to `User("Bo")`
    val user = User("Bo")
    // Print "${admin.name}: ${admin.role}" to the console, with a trailing newline
    println("${admin.name}: ${admin.role}")
    // Print "${user.name}: ${user.role}" to the console, with a trailing newline
    println("${user.name}: ${user.role}")
}
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. यह भूल जाना कि जब एक primary constructor मौजूद हो तो secondary constructor को : this(...) से इसे delegate करना चाहिए।
  2. shared setup को primary constructor या एक init block में रखने के बजाय कई secondary constructors में validation logic दोहराना।
  3. constructor parameters को properties के साथ confuse करना; बिना val/var के एक साधारण parameter constructor के बाहर accessible नहीं है जब तक store न किया जाए।
चैप्टर सारांश
  • Primary constructor class header में, class name के ठीक बाद declare होता है।
  • Secondary constructors class body के अंदर constructor keyword से declare होते हैं और उन्हें primary constructor को delegate करना चाहिए।
  • बिना val/var के constructor parameters सिर्फ constructor/init block के अंदर उपयोग होने योग्य हैं, class members के रूप में नहीं।
  • कई secondary constructors किसी class को common setup logic share करते हुए अलग-अलग तरीकों से बनने देते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.