官术网_书友最值得收藏!

  • Scala Programming Projects
  • Mikael Valot Nicolas Jorand
  • 228字
  • 2021-07-23 16:25:16

Creating the Main object

Here we are! Let's create our first application. First, create the entry point for the program. If you are coming from Java, it would be equivalent to defining the public static void main(String[] args).

Right-click on the src/main/scala folder and select New | Scala Class. Give Main as the class name and Object as the Kind:

We have created our first object. This object is a singleton. There can be only one instance of it in the JVM. The equivalent in Java would be a static class with static methods.

We would like to use it as the main entry point of our program. Scala provides a convenient class named App that needs to be extended. Let's extend our Main object with that class:

object Main extends App {

}

The App superclass defines a static main method that will execute all the code defined inside your Main object. That's all – we created our first version, which does nothing!

We can now run the program in IntelliJ. Click on the small green triangle in the gutter of the object definition, as follows:

The program gets compiled and executed, as shown in the following screenshot:

It is not spectacular, but let's improve it. To get the right habits, we are going to use the TDD technique to proceed further.

主站蜘蛛池模板: 当雄县| 依兰县| 奉化市| 巴彦淖尔市| 保康县| 清流县| 文成县| 连山| 井研县| 香港 | 太保市| 文安县| 安西县| 盐津县| 五华县| 临猗县| 绵竹市| 竹山县| 嘉禾县| 库伦旗| 灵山县| 岢岚县| 囊谦县| 馆陶县| 济宁市| 衡水市| 洪洞县| 长葛市| 乌鲁木齐市| 高邮市| 资溪县| 上思县| 绥德县| 秀山| 海原县| 庆阳市| 色达县| 宁晋县| 久治县| 资源县| 监利县|