O'Reilly logo

Scala for Java Developers: A Practical Primer by Toby Weston

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Toby Weston 2018
Toby WestonScala for Java Developershttps://doi.org/10.1007/978-1-4842-3108-1_9

9. Classes and Objects

Toby Weston
(1)
London, UK
 
In this chapter we’ll look at the following:
  • How you can define fields within the class body rather than on the class definition line and how this affects the generated methods.
  • How you create additional constructors.
  • Scala’s singleton objects defined with the object keyword.
  • Companion objects, a special type of singleton object.

Classes Without Constructor Arguments

Let’s begin by looking at how we create fields within classes without defining them on the class definition line. If you were to create a class in Scala with no fields defined on the class definition, like this:
  // scala
  class Counter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required