Scala - new vs object extends
scala
Solution
As a practical matter, `object` declarations are initialized with the same mechanism as `new` in the bytecode. However, there are quite a few differences:
- `object` as singletons -- each belongs to a class of which only one instance exists;
- `object` is lazily initialized -- they'll only be created/initialized when first referred to;
- an `object` and a `class` (or `trait`) of the same name are companions;
- methods defined on `object` generate static forwarders on the companion `class`;
- members of the `object` can access private members of the companion `class`;
- when searching for implicits, companion objects of relevant* classes or traits are looked into.
These are just some of the differences that I can think of right of the bat. There are probably others.
* What are the "relevant" classes or traits is a longer story -- look up questions on Stack Overflow that explain it if you are interested. Look at the wiki for the `scala` tag if you have trouble finding them.
Problem
What is the difference between defining an object using the new operator vs defining a standalone object by extending the class? More specifically, given the type `class GenericType { ... }`, what is the difference between `val a = new GenericType` and `object a extends GenericType`?