← Back to Scala Course | Chapter 1: Setup & Basics | Lesson 5 of 8

Program की Structure

एक basic Scala program एक object के अंदर रहता है जिसमें main नाम की एक शुरुआती जगह होती है। यह उस stage जैसा है जिसके दरवाज़े पर लिखा हो 'यहां से शो शुरू होता है'।

In this page:

  1. Program Structure
Syntax
scala
object ObjectName {
  def main(args: Array[String]): Unit = {
    // program code
  }
}

Program Structure

एक basic Scala program एक object के रूप में व्यवस्थित होता है जिसमें एक main method होता है (या App को extend करता है), और statements क्रम में execute होते हैं।

object Main extends App शॉर्टहैंड आपको explicit main method में लपेटने के बजाय top-level statements सीधे object के body में लिखने देता है। दोनों styles एक चलने योग्य program में compile होती हैं।

Note: scripts और छोटे programs के लिए extends App सुविधाजनक है, लेकिन बड़े, production Scala code में explicit def main(args: Array[String]): Unit ज़्यादा आम है।

उदाहरण: Program Structure

markup
object Main {
  def main(args: Array[String]): Unit = {
    println("Program started")
    greet()
    println("Program finished")
  }

  def greet(): Unit = {
    println("Hello from a helper method!")
  }
}
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. object के बजाय class के अंदर def main लिखना, जिससे Scala को static entry point नहीं मिलता।
  2. def main(args: Array[String]) को बिना Unit return type या गलत signature के साथ लिखना, जिससे program entry point के रूप में नहीं चलता।
  3. greet() को define होने से पहले call करना और यह मान लेना कि order मायने रखता है, जबकि किसी object के अंदर methods किसी भी order में define हो सकते हैं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.