2

In Java I understand class fields as variables that are accessible from the whole everywhere in the class and that kind of describe the state-structure of instances. In Java field are defined outside any method (and this is the only thing that can be outside of methods).

In Scala "outside any method" is the main constructor - in other words: there is no "outside any method". Thus fields are defined in the main constructor. Thus any variable/value in the constructor is a field. Even arguments given to the constructor are automatically class fields and not local constructor variables as in Java.

In case I got all that right: Are there local variables/values in Scala constructors?

If not: Why was it decided that such a thing is not needed?

Clarficiation: I ask about concepts, not a specific case. Also I do not ask about how to work around to get something like local variables (although I would appreciate it if the answer were that there aren't any).

4
  • 2
    Possible duplicate of Constructor-local variables in Scala Commented Oct 4, 2016 at 14:42
  • @AleksandarStojadinovic: Have you read the other question? As far as I understand that is a specific case and my question is about concepts. Commented Oct 4, 2016 at 14:51
  • I thought your question was "are there such variables", and the answer from the other question is:"No". Or am I missing something? Commented Oct 4, 2016 at 15:02
  • @AleksandarStojadinovic: Where do you read in the other question's answers that there is no such Thing as local variables for constructors? Also: Why was it decided that such a thing is not needed? Commented Oct 4, 2016 at 15:07

1 Answer 1

2

The entire class body is "the contructor".

You can always restrict the scope of any variable to be a small as you like with a pair of braces, so there is no reason to introduce an additional "concept", that serves no specific purpose. Occam's razor.

  class Foo(bar: String) { // constructor parameter
    val baz = "baz";  // class member
    {
      val bat = "bat" // "local" variable
      println(bar + baz + bat) // all three are visible
    }
    println(bar + baz) // only first two are accessble
  }
  println (new Foo("bar").baz) // only class member can be accessed here
Sign up to request clarification or add additional context in comments.

10 Comments

But local variables exist in auxiliary constructors, right? Is it simply that the syntax makes them possible or is there are reason why there should be local variables?
@Make42 "local variable" exist anywhere between a pair of braces. If you write an auxiliary constructor like this def this() = foo it can't have local variables. If you write it like this def this() = { val foo = 1; bar() } then it can. It's not about the type of constructor, it's about the braces. Put them in the main constructor - you can have local variables too. Don't put them in - you can't.
Actually, an auxiliary constructor always has to be of the form def this(...) = { this(...); ... }. You can however write def this() = this({val foo = 1; foo}).
@Jasper-M how about def this() = this("foo", "bar", "baz") ? ;)
@Dima You can fill in those dots with whatever you like :p
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.