Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:18:18+00:00 2026-05-23T14:18:18+00:00

My Scala version: Scala code runner version 2.9.0.1 — Copyright 2002-2011, LAMP/EPFL Given this

  • 0

My Scala version:

Scala code runner version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL

Given this code in a file named Static.scala:

object Main extends App {
  val x: String = "Hello, World!"
  override def main(args: Array[String]): Unit = {
    println(x)
  }
}

When I run:

scalac Static.scala && scala Main

I see:

null

instead of:

Hello, World!

I thought that x was a static because it was defined in an object, and that it would be initialized prior to the main method being called.

What am I doing wrong? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:18:18+00:00Added an answer on May 23, 2026 at 2:18 pm

    This happens because of how you used the App trait. The App trait uses the DelayedInit feature which changes how the class body (including field initialization) is executed. The App trait includes a main method which executes the class body after binding the args field to the arguments to the program. Since you have overridden main this initialization is not happening. You should not provide a main method when using the App trait. You should either put the application code directly in the class body or not inherit from the App trait.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Scala ( Scala code runner version 2.7.7.final ), and I really
This is ridiculous. I am trying to download some code from scala trac: http://lampsvn.epfl.ch/trac/scala/browser/scala-tool-support/trunk/src/vim
consider this piece of code: Welcome to Scala version 2.8.0.r0-b20100714201327 (Java HotSpot(TM) 64-Bit Server
This is the same question for older version of Scala, but they say that
I create a scala source file test.scala : // the source code in test.scala
I have some Scala code roughly analogous to this: object Foo { val thingA
I came across this really nice looking Scala code while researching XMPP for a
Does Scala have a version of Rubys' each_slice from the Array class?
I have to translate the following code from Java to Scala: EDIT: added if-statements
I found an error in my scala code, which puzzles me. Below is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.