DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Trending

  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Beyond the Endpoint: How Agent Cards Enable Discovery, Interoperability, and Trust
  • Delta Lake UniForm to Snowflake: Cross-Platform Table Access Without a Second Copy

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. A Look at the Scala Main Class

A Look at the Scala Main Class

Check out this quick look at adding a main class in Scala.

By 
Emmanouil Gkatziouras
DZone Core CORE ·
Jan. 17, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Check out this quick look at adding a main class in Scala.

Adding a main class in Scala is something that I always end up searching online, so next time, it shall be through this blog post! Let's get started.

You may also like: [DZone Refcard] Getting Started With Scala

Go for the Extends App Option

One way is to add aMainClass by extending theAppclass. Everything else that gets executed on that block is part of the "main" function.

Java




x


 
1
package com.gkatzioura
2
 
          
3
object MainClass extends App {
4
 
          
5
  println("Hello world"!)
6
}



Then, you can access the arguments since they are a variable on the App.

Java




xxxxxxxxxx
1
10
9


 
1
package com.gkatzioura
2
 
          
3
object MainClass extends App {
4
 
          
5
  for( arg <- args ) {
6
    println(arg)
7
  }
8
 
          
9
}




Add a Main Method

This is the most Java familiar option:

Java




xxxxxxxxxx
1


 
1
package com.gkatzioura
2
 
          
3
object MainClass {
4
 
          
5
  def main(args: Array[String]): Unit = {
6
    println("Hello, world!")
7
  }
8
 
          
9
}



As expected, you receive the program arguments through the function arguments.

Java




xxxxxxxxxx
1
11


 
1
package com.gkatzioura
2
 
          
3
object MainClass {
4
 
          
5
  def main(args: Array[String]): Unit = {
6
    for( arg <- args ) {
7
      println(arg)
8
    }
9
  }
10
 
          
11
}



Hope you enjoyed! Please share your thoughts and questions in the comments section!

Further Reading

[DZone Refcard] Getting Started With Scala

Scala in a Java Maven Project

Making Spring Web Services With Scala

Scala (programming language)

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook