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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:41:30+00:00 2026-05-16T03:41:30+00:00

What is the difference between declaring a field as val , lazy val and

  • 0

What is the difference between declaring a field as val, lazy val and object inside a scala class, as in the following snippet:

class A

class B {
  val a1 = new A      { def foo = 1 }
  object a2 extends A { def foo = 1 }
  lazy val a3 = new A { def foo = 1 }
}
  • 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-16T03:41:31+00:00Added an answer on May 16, 2026 at 3:41 am

    In the former, any code included is executed as soon as class B is created. In the latter, however, until you actually use the object, it won’t be instantiated.

    You can see the difference here:

    class A { println("Creating a new A") }
    class B {
      val a1 = new A { println("a1"); def foo = 1 }
      object a2 extends A { println("a2"); def foo = 1 }
    }
    
    scala> val b = new B
    Creating a new A
    a1
    b: B = B@1176e8a
    
    scala> b.a2.foo
    Creating a new A
    a2
    res0: Int = 1
    

    There are also hidden differences in what the created .class files are named and such; and of course the two have different types.

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

Sidebar

Related Questions

In PHP, What is the difference between declaring methods inside class like public function
What's the difference between declaring internal variables inside a JavaScript class with this vs
Recently I've been thinking about performance difference between class field members and method variables.
What is the difference between these two: Declaring the base class function virtual and
Is there any difference between these methods of declaring and setting a public class
When using classic ASP, what is the difference between declaring connection via object tag:
What's the real difference between declaring an array like this: var myArray = new
Is there any difference between declaring inherited virtual function in a child class with
Duplicate Whats the difference between declaring as new and as something new something i
Please can any one tell me whats the difference between declaring a variable inside

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.