Singleton Object vs Companion Object in Class

companion object scala
scala singleton object with parameters
companion objects kotlin
scala singleton object initialization
singleton object vs companion object in scala
companion object in java
scala companion object access class fields
scala class vs object vs trait

I have written the following code:

class a {

  object c {
    var a = "STATIC"

    def m() = print("STATIC METHOD")

    var f = () => print("STATIC FUNCTION")
  }

}

object m {
  def main(args: Array[String]) = {
    var o = new a()
    o.c.m()
  }
}
  1. Can I say that the variables, functions and methods that are declared in object c can be static?
  2. If I change name of object c with a then will the object becomes a companion object?

Scala has no true meaning of 'static' that Java does.

The fact that objects have a backing on the JVM that uses static methods / fields is a leaking implementation detail that you only need to deal with if using Java/JVM interop.

Unless you explicitly need that interop, you need to stop thinking of declared objects as 'static' and instead think of them as singletons within their given scope.

An inner object nested under a class, means that there is only ever going to be 1 instance of that object, for each class instance, unlike inner classes which could have multiple instances.

This applies at the top level as well, except that Scala can do additional compatibility with other JVM languages, and mark some of the methods/members as static.

Scala Singleton and Companion Object, to a particular class, and thus has access to it's private level methods and properties. A Singleton object is an object which defines a single object of a class. A singleton object provides an entry point to your program execution. If you do not create a singleton object in your program, then your code compile successfully but does not give output.

Fields and methods in an object are how Scala declares things that you would have used static for in Java. I guess you can, for intuition sake, say that those fields are usually static (as in only one of those in a JVM at once).

However, in your example, you put an object inside a class, making it no longer static. You can easily check this with a few lines of code (that you can find here on Scastie).

class MyClass {

  object Embedded {
    val a = "field"
    def m = println("method")
  }

}

val a = new MyClass().Embedded
val b = new MyClass().Embedded

// prints "a and b are different objects"
if (a eq b)
  println("a and b are the same object")
else  
  println("a and b are different objects")

Regarding your second question: no, class and object must be in the same scope in order for it to be a companion object. You can find more details on the Scala Language Specification.

I quote from there:

Generally, a companion module of a class is an object which has the same name as the class and is defined in the same scope and compilation unit. Conversely, the class is called the companion class of the module.

What is Singleton?, Scala has no true meaning of 'static' that Java does. The fact that objects have a backing on the JVM that uses static methods / fields is a  A Singleton object is a type of class for which only one instance is created. The syntax of a singleton object is same as a class but instead of using Class keyword we use ‘object’. Lets look at the HOW WHY & WHEN. How to create a Scala Singleton Object. As mentioned above the syntax is same as that of a class but we use the keyword object.

To answer you questions:

  1. The methods and fields in a.c are not globally static because they need an instance of a to exist. If a were an object, a.c would be static too.

  2. If you want to have a companion object with static fields and methods for your class a it has to be defined outside of a's code block, like this:

    class a {
      /* non-static stuff goes here */
    }
    object a {
      /* static stuff goes there */
      def m() = print("STATIC METHOD")
    }
    

You must keep both in the same file, defining the object or the class first doesn't matter, so it generally depend on a convention or what makes most sense depending on use case. If you want to call the static method a.m inside the class a, you will still need to call it a.m and not just m. But the class a will be able to use private fields and methods of object a, because they are companions.

As others already said, static doesn't really exist in Scala, but the concept transpires from Java since Scala is in most cases compiled into java bytecode.

Last advice, the convention is usually the same in Scala and in Java for classes and object: the first-letter of their name should be uppercase (except in some advanced Scala cases)

Companion Objects | Scala Book, Instead of static keyword Scala has singleton object. A Singleton object is an object which defines a single object of a class. A singleton object provides an entry  Singleton object is an object which is declared by using object keyword instead by class. No object is required to call methods declared inside singleton object. In scala, there is no static concept. So scala creates a singleton object to provide entry point for your program execution. If you don't create singleton object, your code will compile successfully but will not produce any output.

What is the advantage of "companion object" vs static keyword , Objects and companion objects. Object declarations. If you need a singleton - a class that only has got one instance - you can declare the class in the  Coming from singleton objects, we now discuss companion objects. A Scala companion object is an object with the same name as a class. We can call it the object’s companion class. The companion class-object pair is to be in a single source file. Either member of the pair can access its companion’s private members.

Singleton Object vs Companion Object in Class, Scala classes cannot have static variables or methods. Instead a Scala class can have what is called a singleton object, or sometime a  An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val. As a top-level value, an object is a singleton. As a member of an enclosing class or as a local value, it behaves exactly like a lazy val. Defining a singleton object. An object is a value.

Scala Singleton and Companion Objects, Singleton object is an object which is declared by using object keyword instead by class. No object is required to call methods declared inside singleton object. In​  An object class can contain properties, functions and the init method. The constructor method is NOT allowed. A singleton object can be defined inside a class. It cannot be defined inside an inner class. An object cannot be instantiated in the way a class is instantiated.

Comments
  • Why are you trying to fit a concept from Java as it is in Scala?. static is not a general concept, it is very specific feature of Java and has no general meaning out side of Java. You can not and "should not" say that some method in Scala is static (or is not static) because static has no meaning in Scala.