简体   繁体   中英

Scala: companion objects and “new” keyword

In my recent posts about using or omitting a "new" keyword in Scala ( "new" keyword in Scala ) I was told that the omission comes from the fact that certain classes have companion objects defined with apply method on them. My question is: are we able to tell or is there any general rule to distinguish which classes/objects have a companion object and apply method?

Thanks in advance and sorry of it's a stupid question, but coming from a Java background it is a bit confusing.

In the Scala API documentation , you see a little icon 在此输入图像描述 in the list on the left side. If you click on that, you go to the documentation of the companion object of the class or trait that's mentioned.

Most of the collection classes and many other classes in the standard library have companion objects with apply methods.

For case classes, the Scala compiler automatically creates a companion object with an apply method (as well as other methods).

In the scala API documentation , you can access the companion object by clicking on the icon 对象图标 as explained by Jesper , but you can also switch between a class and its companion object by clicking on the class/object symbol on the top of the main panel:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM