What is the use of a Scala (singleton) object against typical class-level artifacts

I understand that one of the main advantages of using an object is that they are real objects, and not the functionality of the system. But finally, these objects are also available in the system.

In addition to being more “clean,” this offers the added benefit of scala “objects.”

I am sure there is a number, but I can not figure out which one.

+3
source share
5 answers
  • The objects
  • are independent entities, for example. they can be used as arguments of a method, as a target for implicit conversions and case objects in comparison with a pattern ...
  • , :

.

object X {
  private[this] val z=1 
}

class X { 
  import X._ 
  //won't compile 
  println(z)
}
+13

, , - , . Java , .

Java:

class StaticJava {

  static String AA = "Static String";

  StaticJava() {
  }

}

StaticJava, AA , .

Scala:

object StaticScala {
  val AA = "Static String"
}

class StaticScala

StaticScala StaticScala $, StaticScala singleton.

+2

, . , , .

+2

: ( ).

, , .

, , , , , , , orrible getInstance(). .

, concurrency.

+1

, . - , . , ( , ).

, .

, Java . Scala .

-2

Source: https://habr.com/ru/post/1781289/


All Articles