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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Upgrading Spark Pipelines Code: A Comprehensive Guide
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech

Trending

  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • GDPR Compliance With .NET: Securing Data the Right Way
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  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 user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Jan. 17, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
9.5K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • Upgrading Spark Pipelines Code: A Comprehensive Guide
  • Java vs. Scala: Comparative Analysis for Backend Development in Fintech

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!