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 6687489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:17:42+00:00 2026-05-26T05:17:42+00:00

Consider this snippet: object A { val b = c val c = foo

  • 0

Consider this snippet:

 object A {
     val b = c
     val c = "foo"
 }
 println( A.b )   // prints "null"

As part of a larger program, this would lead to a failure at runtime. The compiler apparently permits the forward reference from ‘b’ to (uninitialized) ‘c’ but ‘b’ is left with c’s original null value. Why is this permitted? Are there programming scenarios that would benefit from this feature?

Change the code to a straight sequence and the behavior changes:

 val b = c
 val c = "foo"
 println( b )   // prints "foo"

Why is the behavior different? And why does this even work? Thanks.

Update 1:

The question came up how I ran the second example. I simplified the setup a bit and compiled it using Scala 2.9.0.1 inside IntelliJ IDEA 10.5.2 with the latest Scala plugin. Here is the exact code, in a freshly created and otherwise empty project, which I am using to test this, which compiles and runs fine in this environment:

 package test
 object Main { 
    def main( args: Array[String] ) {
       val b = c
       val c = "foo"
       println( b )   // prints "foo"
    }
 }

For what it’s worth, IDEA also thinks (when I click “through” the reference to ‘c’ in val b = c) that I am referring to the (later) declaration of ‘c’.

  • 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-26T05:17:43+00:00Added an answer on May 26, 2026 at 5:17 am

    The body of a class or an object is the primary constructor. A constructor, like a method, is a sequence of statements that are executed in order — to do anything else, it would have to be a very different language. I’m pretty sure you wouldn’t like for Scala to execute the statements of your methods in any other order than sequential.

    The problem here is that the body of classes and objects are also the declaration of members, and this is the source of your confusion. You see val declarations as being precisely that: a declarative form of programming, like a Prolog program or an XML configuration file. But they are really two things:

    // This is the declarative part
    object A {
      val b
      val c
    }
    
    // This is the constructor part
    object A {
      b = c
      c = "foo"
    }
    

    Another part of your problem is that your example is very simple. It is a special case in which a certain behavior seems to make sense. But consider something like:

    abstract class A {
      def c: String
    }
    
    class B extends A {
      val b = c
      override val c = "foo"
    }
    
    class C extends { override val c = "foobar" } with B
    
    val x = new C
    println(x.b)
    println(x.c)
    

    What do you expect to happen? The semantics of constructor execution guarantees two things:

    1. Predictability. You might find it non-intuitive at first, but the rules are clear and relatively easy to follow.
    2. Subclasses can depend on superclasses having initialized themselves (and, therefore, its methods being available).

    Output:

    it will print “foobar” twice for more => https://docs.scala-lang.org/tutorials/FAQ/initialization-order.html

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

Sidebar

Related Questions

Consider this snippet: private void button1_Click(object sender, EventArgs e) { if (textBox1.MaxLength > 0)
Consider this C# snippet: static string input = null; static string output = null;
Consider the following code: object U { def foo(s:String) = true } val boolType
Consider this snippet: globalVar = 25 def myfunc(paramVar): localVar = 30 print Vars: {globalVar},
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Consider this Python snippet: for a in range(10): if a == 7: pass if
Consider the following snippet: $(document).bind('mousemove', function(e) { $('#someDiv').css({left: e.pageX+'px', top: e.pageY+'px'}); }); This should
Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Consider this Python program which uses PyGtk and Hippo Canvas to display a clickable

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.