1

How does a main method gets called in scala ? Why does a main method gets called in when it is written only in object but not in class ?

USER
  • 67
  • 4
  • Your assumption is wrong. Main methods can be written outside an object, in a class. – Maroun May 28 '18 at 15:15
  • @Maroun since when that? – Andrey Tyukin May 28 '18 at 15:16
  • @AndreyTyukin It's not possible to define your own method, called `main` inside a class? I'm not Scala expert, but familiar a bit with the language. – Maroun May 28 '18 at 15:18
  • @Maroun It's certainly possible, but I see no reason why it should be treated as an entry point of the application. It will be just an ordinary method that accidentally carries the name "main", but that's not what is usually understood under "main method" on the JVM. The [spec](https://scala-lang.org/files/archive/spec/2.12/09-top-level-definitions.html#programs) talks about objects, and about mixing in the `main` method into objects. Not about classes. – Andrey Tyukin May 28 '18 at 15:20
  • I'm not sure what the question is actually asking. What would be a possible answer? *"Because the spec says so."*? OK, that's the convention, now what's the question? – Andrey Tyukin May 28 '18 at 15:22
  • Here is a [closely related question](https://stackoverflow.com/questions/23416536/main-method-in-scala) that focuses on the "how" from the point of view of translating the method into something that directly corresponds to the `public static void main` in Java. – Andrey Tyukin May 28 '18 at 15:31
  • Hello everyone my question is why does main method (method in which we call methods of other classes/ create objects ) needs to be written in object only . If main method is written a class it gets compiled but why doesnot it gets execute ? – USER May 28 '18 at 15:34
  • If you had class with a `main` method, how would the JVM know with which parameters to instantiate the class in order to call the method on it? – Jasper-M May 28 '18 at 20:49

3 Answers3

5

Because the specification says so:

A program is a top-level object that has a member method main of type (Array[String])Unit. Programs can be executed from a command shell. The program's command arguments are passed to the main method as a parameter of type Array[String].

The main method of a program can be directly defined in the object, or it can be inherited.

It speaks only about top-level objects, not classes. If you define a main method in a class, then it will be just an ordinary method that you can invoke on the instances of this class. Unless you define a top-level object that inherits the main from this class / trait, this method called main will not be treated as an entry point of the application.

Community
  • 1
  • 1
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
3

The main method must be a static method. In Scala to create a static method you put it in an object. Methods in a class are not static.

In the scala language they decided to separate class, which hold only instance behavior and state, and object which hold static behavior and state. This is different from java where classes hold both instance and static members where something is made static using the static keyword.

puhlen
  • 8,400
  • 1
  • 16
  • 31
  • 1 more dout in java main method is static so jvm does not needs to create its object for calling . Does the same thing happens in scala when we write method in object ? – USER May 29 '18 at 05:59
  • Are you asking about the Scala Language Specification or private internal implementation details of the JVM implementation of Scala or some other implementation of Scala (like Scala-native, Scala.js, or Scala.NET)? – Jörg W Mittag May 29 '18 at 16:54
0

It is because in scala the only way to define a method static is object class. And also it is necessary only one instance of main class is created , not multiple instances. That's why it is object class

Tutu Kumari
  • 485
  • 4
  • 10